Giter Site home page Giter Site logo

tau-api-testing's Introduction

tau-api-testing

Training resource to be used alongside "API Testing with Supertest & Nock" on Test Automation University.

Run Tests

npm test

tau-api-testing's People

Contributors

lewisprescott707 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

tau-api-testing's Issues

Conflict between "import" and "require()"

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:

  1. Adding "type": "module" to the main app's package.json files, then importing "require()" using { createRequire }

Gives [ERR_REQUIRE_ESM] : require() of ES Module... not supported

  1. Adding "type": "module" to combinations of two different package.json files, then trying to convert "require()" to "import statements, but it seems the get.spec.js file never runs, and I get the following error:

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:
image
image

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.