-1

I am unable to run the automated tests using jest, supertest and typescript. I am not able to get a simple test to run. All my packages are latest. Please find my package.json, signup test and setup file below

package.json

{
  "name": "auth",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "start": "ts-node-dev src/index.ts",
    "test": "jest --watchAll --no-cache"
  },
  "jest": {
    "preset": "ts-jest",
    "testEnvironment": "node",
    "setupFilesAfterEnv": [
      "./src/test/setup.ts"
    ]
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "@pcblog/common": "^1.0.3",
    "@types/cookie-session": "^2.0.43",
    "@types/express": "^4.17.13",
    "@types/jsonwebtoken": "^8.5.5",
    "@types/mongoose": "^5.11.97",
    "cookie-session": "^1.4.0",
    "express": "^4.17.1",
    "express-async-errors": "^3.1.1",
    "express-validator": "^6.12.2",
    "jsonwebtoken": "^8.5.1",
    "mongoose": "^6.3.5",
    "ts-node-dev": "^1.1.8",
    "typescript": "^4.7.3"
  },
  "devDependencies": {
    "@types/jest": "^25.2.3",
    "@types/supertest": "^2.0.12",
    "jest": "^25.5.4",
    "mongodb-memory-server": "^6.9.6",
    "supertest": "^4.0.2",
    "ts-jest": "^25.5.1"
  }
}

The setup.ts file is next

import request from 'supertest';
import { MongoMemoryServer } from 'mongodb-memory-server';
import mongoose from 'mongoose';
import { app } from '../app';

declare global {
    namespace NodeJS {
        interface Global {
            signin(): Promise<string[]>;
        }
    }
}

let mongo: any;
beforeAll(async () => {
    process.env.JWT_KEY = 'asdfadfas';

    mongo = MongoMemoryServer.create();
    
    const mongoURI = mongo.getUri();

    await mongoose.connect(mongoURI);
});

beforeEach(async () => {
    const collections = await mongoose.connection.db.collections();

    for (let collection of collections) {
        await collection.deleteMany({});
    }
});

afterAll(async () => {
    mongo.stop();
    await mongoose.connection.close();
});

The final file is the test in test folder

import request from 'supertest';
import {app} from '../../app';

it('returns a 201 on successful signup', ()=> {
    return request(app)
        .post('api/users/signup')
        .send({
            email: 'test@test.com',
            password: 'password'
        })
        .expect(201);
});

When I run npm test, I get the error as attachedenter image description here

1 Answers1

0

"Module did not self-register" message usually suggests either different versions of node are being used at npm install time and run time, or different platforms/architectures are being used, e.g. 32-bit vs 64-bit.

Can you run:

  1. node -v
  2. npm -v
  • node -v gives v16.1.0 and npm -v gives 8.10.0 – Pinakin Chaubal Jun 05 '22 at 12:08
  • I would suggest to remove node_modules folder and run npm install again. Have you tried this? – Tenesh Vignesan Jun 05 '22 at 15:51
  • I removed the node_modules and did an npm install again. Did not help. Got this error console.warn Starting the instance failed, enable debug for more information at node_modules/mongodb-memory-server-core/src/MongoMemoryServer.ts:295:17 FAIL src/routes/__test__/signup.test.ts (8.251 s) ✕ returns a 201 on successful signup (1 ms) ● returns a 201 on successful signup Instance Exited before being ready and without throwing an error! – Pinakin Chaubal Jun 12 '22 at 03:11
  • I suppose now the issue is in instantiating the `mongodb-memory-server`. Please try to install version 7.x.x for `mongodb-memory-server`. Also could you take a look at this ; https://stackoverflow.com/questions/67699988/mongo-memory-server-is-not-starting-at-all – Tenesh Vignesan Jun 17 '22 at 23:37
  • Hi Tenesh, Now I get this error TypeError: Cannot read property 'collections' of undefined 26 | 27 | beforeEach(async () => { > 28 | const collections = await mongoose.connection.db.collections(); | ^ 29 | 30 | for (let collection of collections) { 31 | await collection.deleteMany({}); at src/test/setup.ts:28:54 at src/test/setup.ts:8:71 at Object..__awaiter (src/test/setup.ts:4:12) – Pinakin Chaubal Jun 25 '22 at 03:11
  • Found out that supertest works on local setup but not on gcp shell.... – Pinakin Chaubal Jul 04 '22 at 06:19