Run tests in a native Node.js ESM environment (#13966)

This commit is contained in:
Nicolò Ribaudo
2021-12-03 15:32:58 +01:00
committed by GitHub
parent 578ab22fb7
commit 2d989a983d
159 changed files with 1283 additions and 611 deletions

View File

@@ -1,10 +1,13 @@
import fs from "fs";
import path from "path";
import { fileURLToPath } from "url";
import { createRequire, Module } from "module";
const require = createRequire(import.meta.url);
const testCacheFilename = path.join(
path.dirname(fileURLToPath(import.meta.url)),
".babel",
".cache.babel",
);
const oldBabelDisableCacheValue = process.env.BABEL_DISABLE_CACHE;
@@ -32,14 +35,30 @@ function resetCache() {
process.env.BABEL_DISABLE_CACHE = oldBabelDisableCacheValue;
}
const OLD_JEST_MOCKS = !!jest.doMock;
describe("@babel/register - caching", () => {
describe("cache", () => {
let load, get, setDirty, save;
let consoleWarnSpy;
if (!OLD_JEST_MOCKS) {
let oldModuleCache;
beforeAll(() => {
oldModuleCache = Module._cache;
});
afterAll(() => {
Module._cache = oldModuleCache;
});
}
beforeEach(() => {
// Since lib/cache is a singleton we need to fully reload it
jest.resetModules();
if (OLD_JEST_MOCKS) {
jest.resetModules();
} else {
Module._cache = {};
}
const cache = require("../lib/cache.js");
load = cache.load;