Reputation: 8162
I am new to Jest testing and I wrote a small user.test.js
const mongoose = require('mongoose');
const UserModel = require('../models/User');
const userData = { username: 'TekLoon', email: '[email protected]' };
describe('User Model Test', () => {
beforeAll(async () => {
await mongoose.connect(global.__MONGO_URI__, { useNewUrlParser: true, useCreateIndex: true }, (err) => {
if (err) {
console.error(err);
process.exit(1);
}
});
});
it('create & save user successfully', async () => {
const validUser = new UserModel(userData);
const savedUser = await validUser.save();
// Object Id should be defined when successfully saved to MongoDB.
expect(savedUser._id).toBeDefined();
expect(savedUser.username).toBe(userData.username);
expect(savedUser.email).toBe(userData.email);
});
});
Directory structure
globalConfig.json
index.js
models
node_modules
package.json
package-lock.json
__tests__
When I run npm run test
FAIL .history/__tests__/user.test_20201027153457.js
● Test suite failed to run
Jest encountered an unexpected token
Details:
SyntaxError: /home/milenko/pract/post/.history/__tests__/user.test_20201027153457.js: Unexpected token (26:7)
24 | expect(savedUser.username).toBe(userData.username);
25 | expect(savedUser.email).toBe(userData.email);
> 26 | });
| ^
I followed Christian's advice,problems are again here
FAIL __tests__/jest-mongodb-config.js
● Test suite failed to run
Your test suite must contain at least one test.
at onResult (node_modules/@jest/core/build/TestScheduler.js:175:18)
at node_modules/@jest/core/build/TestScheduler.js:304:17
at node_modules/emittery/index.js:260:13
at Array.map (<anonymous>)
at Emittery.Typed.emit (node_modules/emittery/index.js:258:23)
FAIL __tests__/jest.config.js
● Test suite failed to run
Your test suite must contain at least one test.
at onResult (node_modules/@jest/core/build/TestScheduler.js:175:18)
at node_modules/@jest/core/build/TestScheduler.js:304:17
at node_modules/emittery/index.js:260:13
at Array.map (<anonymous>)
at Emittery.Typed.emit (node_modules/emittery/index.js:258:23)
Comma expected?
How to fix this?
Upvotes: 1
Views: 698
Reputation: 8162
I managed to solve this problem. Compilers behaviour was somehow misleading. The solution was to put
jest.config.js
with this content
module.exports = {
preset: '@shelf/jest-mongodb',
};
and jest-mongodb-config.js
module.exports = {
mongodbMemoryServerOptions: {
instance: {
dbName: 'jest'
},
binary: {
version: '4.4.1',
skipMD5: true
},
autoStart: false
}
};
in PROJECT directory( tests is reserved only for test files). Works fine now(simple example)
tests__/user.test.js
User Model Test
✓ create & save user successfully (19 ms)
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 0.895 s, estimated 2 s
Ran all test suites.
Upvotes: 0
Reputation: 7852
You need to add closing parenthesis for describe
and beforeAll
const mongoose = require('mongoose');
const UserModel = require('../models/User');
const userData = { username: 'TekLoon', email:'[email protected]' };
describe('User Model Test', () => {
beforeAll(async () => {
await mongoose.connect(global.__MONGO_URI__, { useNewUrlParser: true, useCreateIndex: true }, (err) => {
if (err) {
console.error(err);
process.exit(1);
}
});
}) // added closing parenthesis
it('create & save user successfully', async () => {
const validUser = new UserModel(userData);
const savedUser = await validUser.save();
// Object Id should be defined when successfully saved to MongoDB.
expect(savedUser._id).toBeDefined();
expect(savedUser.username).toBe(userData.username);
expect(savedUser.email).toBe(userData.email);
});
}); // added closing parenthesis
Upvotes: 1