Training resource to be used alongside "API Testing with Supertest & Nock" on Test Automation University.
npm test
Test Automation University - API Testing with Supertest & Nock
License: MIT License
Hi Lewis
Thanks heaps for your amazing training course on TAU. I'm really quite new to testing, development, and APIs etc, so your tutorial is super valuable to me!
I've been struggling with this issue for more than a day now and am not really sure how to address this, and was hoping you might be able to help.
I'll write detailed steps on what I've tried below, but to summarise, it seems like your application is using ESM "import" statements while the get.spec.js file that I'm creating uses "require()" statements, and the two don't like to coexist together. I've tried:
Gives [ERR_REQUIRE_ESM] : require() of ES Module... not supported
SyntaxError[ .../lewisPrescott-tau-api-supertest/tests/get.spec.js ]: Unexpected identifier
More detailed steps are as follows:
I cloned this project and have been following your tutorial on TAU (https://testautomationu.applitools.com/javascript-api-testing/chapter2.1.html), and noticed that there's a change in the folder system when compared with the tutorial, so I made sure to create the tests folder in the top level directory:
I then created a get.spec.js file under the "tests" folder and used the "require()" statement to pull in the app, "supertest" request, and chai.expect functions to run the test (as per tutorial).
`const app = require('../app/src');
const request = require('supertest');
const expect = require('chai.expect');
describe('get request tests', () => {
it('Gets route params', () => {
request(app)
.get('/users/1')
.end((err,res) => {
expect((res.body.id).to.be.equal(1));
});
});
})`
However, the first error it throws is
.../lewisPrescott-tau-api-supertest/app/src/index.js:1
import React from "react";
^^^^^^
SyntaxError: Cannot use import statement outside a module...
I added "type": "module" to .../lewisPrescott-tau-api-supertest/app/package.json, which gave me the error:
Error [ERR_REQUIRE_ESM]: require() of ES Module ... not supported.
There are quite a few articles out there on how this can be fixed including the "change file type to .mjs/.cjs" ones which led me in circles, and the only one that made progress was further adding "type": "module" to .../lewisPrescott-tau-api-supertest/package.json, then trying to "import" the require function using:
import { createRequire } from "module";
const require = createRequire(import.meta.url);
and trying to convert "require()" to an "import" statement:
import app from '../app/src';
import * as request from supertest;
import expect from chai;
Unfortunately, the many combinations of these always end up with a "Unexpected Identifier" error of some form, i.e.:
SyntaxError[ .../lewisPrescott-tau-api-supertest/tests/get.spec.js ]: Unexpected identifier
at ESMLoader.moduleStrategy (node:internal/modules/esm/translators:119:18)
at ESMLoader.moduleProvider (node:internal/modules/esm/loader:468:14)
at async link (node:internal/modules/esm/module_job:67:21)
The best I can narrow it down to is a package conflict, but upgrading packages doesn't seem to work and I'm not really sure how to proceed.. I know you've already taken heaps of time to create this tutorial and I really appreciate it. Is there any chance you'd be willing to help me get this going please?
Many thanks
Chris
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.