Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

setupGlobal global not available when running multiple tests #6408

Closed
maloguertin opened this issue Jun 6, 2018 · 6 comments
Closed

setupGlobal global not available when running multiple tests #6408

maloguertin opened this issue Jun 6, 2018 · 6 comments

Comments

@maloguertin
Copy link
Contributor

🐛 Bug Report

I took inspiration from this: https://facebook.github.io/jest/docs/en/mongodb.html in order to run my MongoDB+Mongoose in memory. When I run the test command on for a specific file:

npm run test User.test.js

everything works fine and the globals are properly assigned.
When i run the test command for all my files:

npm run test

the globals are always undefined in my custom environment setup() method.

To Reproduce

Steps to reproduce the behavior:

my global setup file looks like this:

const MongodbMemoryServer = require('mongodb-memory-server')

const mongoServer = new MongodbMemoryServer.MongoMemoryServer()

module.exports = async () => {
  global.__MONGO_URI__ = await mongoServer.getConnectionString()
}

and then my custom environment:

// mongo-environment
const NodeEnvironment = require('jest-environment-node')

class MongoEnvironment extends NodeEnvironment {
  constructor(config) {
    super(config)
  }

  async setup() {
    console.log(global.__MONGO_URI__)
    this.global.__MONGO_URI__ = global.__MONGO_URI__

    await super.setup()
  }

  async teardown() {
    await super.teardown()
  }

  runScript(script) {
    return super.runScript(script)
  }
}

module.exports = MongoEnvironment

Expected behavior

I should see a console.log() with a string of the mongo uri in the output but I get undefined when running with

npm run test

Run npx envinfo --preset jest

Paste the results here:

npx: installed 1 in 1.044s

  System:
    OS: Linux 4.15 Ubuntu 18.04 LTS (Bionic Beaver)
    CPU: x64 Intel(R) Core(TM) i7-6700HQ CPU @ 2.60GHz
  Binaries:
    Node: 9.2.0 - ~/.nvm/versions/node/v9.2.0/bin/node
    Yarn: 1.3.2 - ~/.nvm/versions/node/v9.2.0/bin/yarn
    npm: 5.5.1 - ~/.nvm/versions/node/v9.2.0/bin/npm
  npmPackages:
    jest: ^23.0.0 => 23.1.0
@SimenB
Copy link
Member

SimenB commented Jun 7, 2018

Duplicate of #6007

@SimenB SimenB marked this as a duplicate of #6007 Jun 7, 2018
@SimenB SimenB closed this as completed Jun 7, 2018
@SimenB
Copy link
Member

SimenB commented Jun 7, 2018

That does remind me that the example is wrong though - it only works with runInBand. @rickhanlonii thoughts on how to best handle it? Maybe write the host and port to disk or something (similar to what jest-puppeteer does), then the custom environment can pick it up and provide it as globals.

@maloguertin
Copy link
Contributor Author

maloguertin commented Jun 7, 2018

@SimenB thanks for the quick answer. The example should propably not advocate to use global in the globalSetup?

for anyone wondering this is how I got around the problem:

const MongodbMemoryServer = require('mongodb-memory-server')

const path = require('path')

const fs = require('fs')

const mongoServer = new MongodbMemoryServer.MongoMemoryServer()

const mongoUriFile = path.join(__dirname, 'mongoUri.txt')

module.exports = async () => {
  const mongoUri = await mongoServer.getConnectionString()
  fs.writeFileSync(mongoUriFile, mongoUri)
}
// mongo-environment
const NodeEnvironment = require('jest-environment-node')

const path = require('path')

const fs = require('fs')

const mongoUriFile = path.join(__dirname, 'mongoUri.txt')

class MongoEnvironment extends NodeEnvironment {
  constructor(config) {
    super(config)
  }

  async setup() {
    this.global.__MONGO_URI__ = fs.readFileSync(mongoUriFile, 'utf-8')

    await super.setup()
  }

  async teardown() {
    await super.teardown()
  }

  runScript(script) {
    return super.runScript(script)
  }
}

module.exports = MongoEnvironment

@sibelius
Copy link

@maloguertin why this works for parallel tests?

@youbek
Copy link

youbek commented Apr 29, 2020

I've come across this exact issue too, and the solution was to store mongo URI in the env.

- global.__MONGO_URI__ = mongoURI;
+ process.env.__MONGO_URI__ = mongoURI;

@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 11, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants