avriis
avriis

Reputation: 1681

What's config/start.js in usage example for jest-dev-server

Trying to setup up jest with jest-dev-server (see here). In the usage example they reference config/start.js. Where/what is this file?

// 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
}

Upvotes: 3

Views: 1674

Answers (2)

Sagan
Sagan

Reputation: 2343

Here is how to use the "jest-dev-server" npm package with Node + JEST, which will start the web server before running your tests.

In your jest.config.js file add this (change file paths as needed):

  // 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",

Now add a globalSetup.js file:

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");
}

Now add a globalTeardown.js file:

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

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

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

Upvotes: 5

Marcin
Marcin

Reputation: 600

Maybe i'm quite late but command: node config/start.js --port=3000 Refers to the command you / your package json uses to start the server.

Usually you will find that command in package.json in "scripts" object.

{
  "name": "<project name>",
  "version": "<ver>",
  "dependencies": {
    "<dependencies list>"
  },
  "scripts": {
    "start": "node scripts/start.js",
  }
...

and the --port=3000 is just port tag that specifies on which port your server should run

Upvotes: 0

Related Questions