简体   繁体   English

如何在所有 jsamine 测试之前运行脚本?

[英]How to run a script before all jsamine tests?

I want to start a server and run tests on its API with Jasmine.我想使用 Jasmine 启动服务器并在其 API 上运行测试。

For that I want to make sure that the server is setup and running before jasmine runs its tests.为此,我想确保在 jasmine 运行其测试之前服务器已设置并运行。

Also I have a lot of tests, and I split them into mutliple files.我也有很多测试,我把它们分成多个文件。

I dont want to particularly start the server in the beforeAll hook of each test file, as it leads to conflicts on the port the server is running.我不想特别在每个测试文件的 beforeAll 挂钩中启动服务器,因为它会导致服务器正在运行的端口发生冲突。

I thought of 2 theoretical Solutions that I dont know how to do with Jasmine.我想到了 2 个我不知道如何处理 Jasmine 的理论解决方案。

  1. To have a global before/after script to the jasmine command which is executed before/after all test-files.在所有测试文件之前/之后执行 jasmine 命令的全局之前/之后脚本。
  2. To have a way to import all test Files into on Jasmine file where I can do my setup in the beforeAll as we all know it.有一种方法可以将所有测试文件导入到 Jasmine 文件中,我可以在 beforeAll 中进行设置,我们都知道。 However I dont know how to properly import those files and it also makes them all dependent on my mainTest-file.但是我不知道如何正确导入这些文件,这也使它们都依赖于我的 mainTest 文件。 Meaning I cant execute them singularly.这意味着我不能单独执行它们。

Additional Info: Im in a node.js environment running a express-server and am testing its api (each route gets its test file)附加信息:我在运行快速服务器的 node.js 环境中并正在测试其 api(每条路线都有其测试文件)

You can use the helpers configuration.您可以使用helpers配置。 The files in the helpers directory will be executed before running all the tests. helpers 目录中的文件将在运行所有测试之前执行。 For example:例如:

Project structure:项目结构:

.
├── .babelrc
├── .editorconfig
├── .gitignore
├── .nycrc
├── .prettierrc
├── LICENSE
├── README.md
├── jasmine.json
├── package-lock.json
├── package.json
└── src
    ├── helpers
    │   ├── console-reporter.js
    │   ├── fake-server-setup.js
    │   └── jsdom.js
    └── stackoverflow
        ├── 60138152
        ├── 61121812
        ├── 61277026
        ├── 61643544
        ├── 61985831
        └── 62172073

fake-server-setup.js : fake-server-setup.js

const express = require('express');

beforeAll((done) => {
  const app = express();
  global.app = app;
  const port = 3000;
  app.get('/api', (req, res) => {
    res.sendStatus(200);
  });
  app.listen(port, () => {
    done();
    console.log('server is listening on port:' + port);
  });
});

We store the app variable which we will use in each test file to the global variable.我们将在每个测试文件中使用的app变量存储到global变量中。

a.test.js : a.test.js

const supertest = require('supertest');
describe('62172073 - a', () => {
  it('should pass', () => {
    return supertest(global.app).get('/api').expect(200);
  });
});

b.test.js : b.test.js

const supertest = require('supertest');
describe('62172073 - b', () => {
  it('should pass', () => {
    return supertest(global.app).get('/api').expect(200);
  });
});

jasmine.json : jasmine.json

{
  "spec_dir": "src",
  "spec_files": ["**/?(*.)+(spec|test).[jt]s?(x)"],
  "helpers": ["helpers/**/*.js", "../node_modules/@babel/register/lib/node.js"],
  "stopSpecOnExpectationFailure": false,
  "random": true
}

Test result:测试结果:

Executing 2 defined specs...
Running in random order... (seed: 03767)

Test Suites & Specs:
(node:54373) ExperimentalWarning: The fs.promises API is experimental

1. 62172073 - bserver is listening on port:3000

   ✔ should pass (51ms)

2. 62172073 - a
   ✔ should pass (5ms)

>> Done!


Summary:

👊  Passed
Suites:  2 of 2
Specs:   2 of 2
Expects: 0 (none executed)
Finished in 0.085 seconds

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

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