2

I'm trying to create some e2e tests via puppeteer and jest, and now I'm stuck with a problems regarding global variables. So I have 2 issues:

  1. How can I use globals in jest with puppeteer? Now I have the following in my jest.config.js
module.exports = {
    globals: {
        URL: "https://demoqa.com/",
        value: "myvalue"
      },
    preset: 'jest-puppeteer',
    testMatch: ["**/?(*.)+(spec|test).[t]s"],
    testPathIgnorePatterns: ['/node_modules/', 'dist'],
    setupFilesAfterEnv: ['<rootDir>/jest.setup.ts'],
    transform: {
        "^.+\\.ts?$": "ts-jest"
    },
    globalSetup: './jest.global-setup.ts',
    globalTeardown: './jest.global-teardown.ts',
    verbose: true
};

and then I'm trying to use globals in my src/tests/test.spec.ts like this:

console.log(URL.toString());
console.log(value.toString());

But this gives error that value is not defined, but URL has the right value as in the jest.config.js

What am I doing wrong?

  1. How can I pass this globals via command line? I found in jest docs the --globals flag, and tried to use it like this:

npm run jest -- --globals URL='someUrl', value='someValue'

but it doesn't work too.

asko
  • 23
  • 1
  • 3

1 Answers1

3
  1. Use the global object (e.g global.value) to access globals in the tests

    console.log(global.value);
    
  2. Use the --globals parameter to override globals in the valid JSON format

    npm run jest -- --globals='{"URL": "someUrl", "value": "someValue"}'
    
Yevhen Laichenkov
  • 7,746
  • 2
  • 27
  • 33
  • When I use it like `global.value` it says 'Property 'value' does not exist on type 'Global & typeof globalThis'.' – asko Apr 12 '21 at 13:48
  • And it throws this when I'm trying to pass JSON string in globals `SyntaxError: Unexpected token U in JSON at position 1` – asko Apr 12 '21 at 13:50
  • Adding `\` before `{` `}` and `"` helps but parameters from JSON are not passed to the code. Why? – asko Apr 12 '21 at 13:53
  • Adding back slash – asko Apr 12 '21 at 14:00
  • Its' a typescript issue. You have to define a custom type for the global. Read more [here](https://stackoverflow.com/questions/35074713/extending-typescript-global-object-in-node-js). Also, you can just use any for the global (global as any). – Yevhen Laichenkov Apr 12 '21 at 14:21
  • It means that you provided the invalid JSON. I've sent a valid and tested it before answering. You need to use quote '' for the JSON. And please, validate your JSON before passing it. https://jsonlint.com/ – Yevhen Laichenkov Apr 12 '21 at 14:23
  • And you don't have to use the `toString()` method for the properties. – Yevhen Laichenkov Apr 12 '21 at 14:25
  • Thanks, the thing with defining a custom type works. But solution with passing json string not, I pass json from your comment and this error with unexpected token pops up. I always vlidate JSONs on the jsonlint.com – asko Apr 13 '21 at 09:37
  • Happy to hear it. Are you running it on Windows? Can you send the whole command with the `JSON` argument? – Yevhen Laichenkov Apr 13 '21 at 10:21
  • I'm running the following command in VS Code terminal in Windows: `npm run test -- --globals='{"URL": "someUrl", "value": "someValue"}' ` and in my packaje.json I have the following script `"scripts": { "test": "jest" },` – asko Apr 13 '21 at 14:33
  • I've finally figured out, but I dunno where I was whrong. So the following script works: ` npm run test -- --globals='{\"URL\":\"https://demoqa.com/\",\"HEADLESS\":true,\"SLOWMO\":10}' ` and also I have the same globals from this script in my jest.config.js. So now all globals are passing in my tests – asko Apr 15 '21 at 09:44