简体   繁体   English

为什么Jest会遇到意想不到的token?

[英]Why did Jest encounter unexpected token?

I am new to Jest testing and I wrote a small user.test.js我是 Jest 测试的新手,我写了一个小 user.test.js

const mongoose = require('mongoose');
const UserModel = require('../models/User');
const userData = { username: 'TekLoon', email: 'jon@aol.com' };

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当我运行 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我听从了 Christian 的建议,问题又来了

 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)

I will add image to highlight我将添加图像以突出显示在此处输入图片说明

Comma expected?逗号预期?

How to fix this?如何解决这个问题?

You need to add closing parenthesis for describe and beforeAll您需要为describebeforeAll添加beforeAll括号

const mongoose = require('mongoose');
const UserModel = require('../models/User');
const userData = { username: 'TekLoon', email:'jon@aol.com' };

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

I managed to solve this problem.我设法解决了这个问题。 Compilers behaviour was somehow misleading.编译器的行为在某种程度上具有误导性。 The solution was to put jest.config.js with this content解决方案是将jest.config.js与此内容

module.exports = {
  preset: '@shelf/jest-mongodb',
};

and jest-mongodb-config.js和 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).在 PROJECT 目录中(测试仅保留用于测试文件)。 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.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM