mercredi 3 mars 2021

Trying to create a singleton unique id before jest testing, called twice instead of once


Hey, I'm currently trying to generate a unique id before compiling a jest testing and for it to be called within the tests and globalSetup and globalTeardown this is my setup

in my package.json

"scripts": {
    ...
    "test:e2e": "cross-env TS_NODE_PROJECT=tsconfig.json NODE_ENV=test && jest --config ./test/jest-e2e.json",
}
....
"jest": {
"moduleFileExtensions": [
  "js",
  "json",
  "ts"
],
"rootDir": "src",
"testRegex": ".spec.ts$",
"transform": {
  "^.+\\.(t|j)s$": "ts-jest"
},
"coverageDirectory": "../coverage",
"testEnvironment": "node"

}

in my jest-e2e.json

{
  "moduleFileExtensions": ["js", "json", "ts"],
  "rootDir": ".",
  "testEnvironment": "node",
  "testRegex": ".e2e-spec.ts$",
  "transform": {
    "^.+\\.(t|j)s$": "ts-jest"
  },
  "moduleNameMapper": {
    "/^src\/(.*)$/": "src/$1"
  },
  "verbose": true,
  "globalSetup": "./globalSetup.ts",
  "globalTeardown": "./modules/globalTeardown.ts"
}

in my config (that will be called within globalSetup, globalTeardown, tests, etc..)

var uniqueSchema = new Date().getTime();

const configuration: Environments = {
  local: {
    schemaName: 'sdb',
  },
  test: {
    schemaName: uniqueSchema,
  }
} as Environments

interface Environments {
  local: any, // should add configuration interface
  dev: any,
  test: any,
  int: any,
  prod: any
}

export default configuration;

when I'm calling the testing config within globalSetup it's a certain value but just before the test itself it's being called again and the value changes within the test itself. and when it's in the tearDown it's the same value as in globalSetup

same goes if i try to generate a uuid.

what am i doing wrong here?

Aucun commentaire:

Enregistrer un commentaire