首页 > 解决方案 > jest-dev-server 的使用示例中的 config/start.js 是什么

问题描述

尝试使用 jest-dev-server 设置 jest(请参见此处)。在使用示例中,它们引用了 config/start.js。这个文件在哪里/是什么?

// global-setup.js
const { setup: setupDevServer } = require('jest-dev-server')

module.exports = async function globalSetup() {
  await setupDevServer({
    command: `node config/start.js --port=3000`,
    launchTimeout: 50000,
    port: 3000,
  })
  // Your global setup
}

标签: node.jsjestjs

解决方案


以下是如何在 Node + JEST 中使用“jest-dev-server”npm 包,它将在运行测试之前启动 Web 服务器。

在你的 jest.config.js 文件中添加这个(根据需要更改文件路径):

  // A path to a module which exports an async function that is triggered once before all test suites
  "globalSetup": "<rootDir>/spec/config/globalSetup.js",

  // A path to a module which exports an async function that is triggered once after all test suites
  "globalTeardown": "<rootDir>/spec/config/globalTeardown.js",

现在添加一个 globalSetup.js 文件:

const { setup: setupDevServer } = require('jest-dev-server')

module.exports = async function globalSetup() {
  await setupDevServer({
    command: 'node entryPointScriptToStartYourWebApp.js',
    launchTimeout: 10000,
    port: 3000
  })

  // Your global setup
  console.log("globalSetup.js was invoked");
}

现在添加一个 globalTeardown.js 文件:

const { teardown: teardownDevServer } = require('jest-dev-server')

module.exports = async function globalTeardown() {
  await teardownDevServer()

  // Your global teardown
  console.log("globalTeardown.js was invoked");
}

推荐阅读