mochiya98 / mocha-chai-jest-snapshot

provides snapshot testing like jest
MIT License
29 stars 5 forks source link

cannot find globals 'before' and 'after' #16

Open mrflip opened 2 years ago

mrflip commented 2 years ago

Could not get this to work; it gives the error ReferenceError: before is not defined.

It traces back to these lines (in the built version)

    return function (chai, utils) {
        before(() => __awaiter(this, void 0, void 0, function* () {
            _manager.snapshotResolver = yield (0, jest_snapshot_1.buildSnapshotResolver)(Object.assign({ transform: [] }, config));
        }));
        beforeEach(function () {
            if (this.currentTest)
                _manager.setContext(this.currentTest);
        });
        after(function () {
            _manager.saveSnap();
            if (!_reporterAttached)
                _manager.report();
        });

Those don't seem to be globals delivered by Jest.

My jest setupFilesAfterEnv.js:

import Chai                             from 'chai'
import ChaiArrays                       from 'chai-arrays'
import ChaiAsPromised                   from 'chai-as-promised'
import ChaiEach                         from 'chai-each'
import Sinon                            from 'sinon'
import ChaiSinon                        from 'sinon-chai'
import * as SnapshotPlugin              from 'mocha-chai-jest-snapshot'

const { jestSnapshotPlugin } = SnapshotPlugin

Chai.use(ChaiArrays)
Chai.use(ChaiEach)
Chai.use(ChaiSinon)
Chai.use(SnapshotPlugin.jestSnapshotPlugin())
Chai.use(ChaiAsPromised) // must be last

global.sinon  = Sinon
global.expect = Chai.expect

Error.stackTraceLimit = 50

and jest.config.cjs:

// For a detailed explanation regarding each configuration property, visit:
// https://jestjs.io/docs/en/configuration.html

module.exports = {
  // Run tests from one or more projects
  projects: [
    {
      displayName: "tests",
      // A list of paths to modules that run some code to configure or set up the testing framework before each test
      setupFilesAfterEnv:       ['./tests/setupFilesAfterEnv.js'],
      testMatch:                ["<rootDir>/tests/**/*.test.[tj]s?(x)"],
      testPathIgnorePatterns:   ["tests\/store", "tests\/.*\/.*Store.*"],
    },
    {
      displayName: "dbtests",
      // A list of paths to modules that run some code to configure or set up the testing framework before each test
      setupFilesAfterEnv:       ['./tests/setupFilesAfterEnv.js'],
      testMatch:                ["<rootDir>/tests/store/*.test.[tj]s?(x)", "<rootDir>/tests/*/.*Store*.test.[tj]s?(x)"],
    },
    // {
    //   displayName: "lint",
    //   runner:      "jest-runner-eslint",
    //   testMatch:   ["<rootDir>/{src,tests}/**/*.{js,ts}"],
    // },
  ],
  //
  // A list of paths to directories that Jest should use to search for files in
  roots: [
    'tests',
    // 'built',
  ],

  // The glob patterns Jest uses to detect test files
  testMatch: [
    "**/tests/**/*.test.[tj]s?(x)",
  ],

  // A list of paths to modules that run some code to configure or set up the testing framework before each test
  setupFilesAfterEnv: ['./tests/setupFilesAfterEnv.js'],

  // The test environment that will be used for testing
  testEnvironment: "node",

  // Indicates whether each individual test should be reported during the run
  verbose: true,

  // The maximum amount of workers used to run your tests. Can be specified as % or a number.
  // E.g. maxWorkers: 10% will use 10% of your CPU amount + 1 as the maximum worker number.
  //      maxWorkers: 2 will use a maximum of 2 workers.
  maxWorkers: "30%",
}
tpluscode commented 1 year ago

A workaround appears to be to initialize the plugin inside a describe

describe('suite', () => {
  chai.use(jestSnapshotPlugin())
})