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