jest is undefined in setupFile

Created on 26 Jul 2017  路  9Comments  路  Source: facebook/jest

Do you want to request a feature or report a bug?
bug
What is the current behavior?
I would like to mock the console with a setup script, like mentioned here
But neither jest nor beforeEach is defined in that file.

What is the expected behavior?
I should be able to mock something in a setupFile

Please provide your exact Jest configuration and mention your Jest, node, yarn/npm version and operating system.
OS: MacOS El Capitan
node: 4.4.3
jest-cli: 20.0.4

       //jest config
        cache: false,
        collectCoverage: false,
        coverageDirectory: 'reports/coverage',
        testMatch: [
            '**/tests/**/*.js?(x)',
            '**/(*.)(test).js?(x)',
        ],
        transform: {
          "^.+\\.js$": '<rootDir>/jest.transform.js',
        },
        setupFiles: ['<rootDir>/jest.setup.js'],
        unmockedModulePathPatterns: ['/node_modules/']
// jest.setup.js

console.log('test', jest);

beforeEach(() => {
  console.trace = jest.fn();
});

// jest.transform.js
/*
 * Custom Jest transform implementation that wraps babel-jest and injects our
 * babel presets, so we don't have to use .babelrc.
 */
module.exports = require('babel-jest').createTransformer({
    presets: [['env', {
        loose: true,
    }]]
});

//output
ReferenceError: jest is not defined

Most helpful comment

so error was that grunt loads all js config files in his config folder befor running... so moving jest.setup.js outside of that folder fixes the problems...
thank you for helping!

All 9 comments

~Yup, it is undefined by design. Use setupTestFrameworkScriptFile to have access to it~
EDIT: should work with setupFiles

same thing with setupTestFrameworkScriptFile:'<rootDir>/jest.setup.js'
ReferenceError: jest is not defined

I made a typo, use the name from the docs

I did, still the same...ReferenceError: jest is not defined

Can you create a repro?

Sorry, it should work for setupFiles! I'll reopen once you provide a repro.

i created a repo and it works as expected - strange...
however, that case is without grunt, so i will add grunt to the testcase and see what happens

so error was that grunt loads all js config files in his config folder befor running... so moving jest.setup.js outside of that folder fixes the problems...
thank you for helping!

Awesome! Keep jesting :)

Was this page helpful?
0 / 5 - 0 ratings