Skip to content

Commit e680a94

Browse files
test(config): read the default-target cases off the environment and the cache keys
The child inherited BROWSERSLIST and BROWSERSLIST_CONFIG, which browserslist reads as a config of their own, so the no-config case answered browserslist where either was set. It now runs without them. It also matched require.cache keys by substring, which no forward-slash entry does on Windows. Asking require.cache by the request's resolved path reads the key Node wrote, so the configured case can assert the whole list.
1 parent f1977e2 commit e680a94

1 file changed

Lines changed: 38 additions & 14 deletions

File tree

‎test/unitCases/target-browserslist.unittest.js‎

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -173,9 +173,33 @@ describe("browserslist target", () => {
173173
const withoutConfig = path.resolve(__dirname, "../fixtures");
174174
const withConfig = path.resolve(__dirname, "../fixtures/browserslist");
175175

176+
// What a browserslist query reads, asked for by request rather than by
177+
// path, so `require.cache` is read with the key Node itself wrote
178+
const QUERY_DATA = [
179+
"browserslist",
180+
"caniuse-lite/dist/unpacker/agents",
181+
"electron-to-chromium/versions",
182+
"node-releases/data/processed/envs.json",
183+
"baseline-browser-mapping"
184+
];
185+
186+
/**
187+
* The environment without the browserslist variables, which decide a
188+
* config of their own and would otherwise reach the child.
189+
* @returns {NodeJS.ProcessEnv} the environment to run a child in
190+
*/
191+
const environmentWithoutBrowserslist = () => {
192+
/** @type {NodeJS.ProcessEnv} */
193+
const environment = {};
194+
for (const [name, value] of Object.entries(process.env)) {
195+
if (!name.startsWith("BROWSERSLIST")) environment[name] = value;
196+
}
197+
return environment;
198+
};
199+
176200
/**
177-
* Resolves the default target in a fresh process, reporting whether the
178-
* data browserslist reads to run a query was loaded to answer it.
201+
* Resolves the default target in a fresh process, reporting which of the
202+
* things a query reads were loaded to answer it.
179203
* @param {string} context the context directory
180204
* @returns {{ target: string, queryData: string[] }} what it answered
181205
*/
@@ -185,20 +209,20 @@ describe("browserslist target", () => {
185209
require.resolve("../../lib/config/target")
186210
)});
187211
const target = getDefaultTarget(${JSON.stringify(context)});
188-
const carriers = [
189-
"browserslist/index.js",
190-
"caniuse-lite/dist/unpacker/agents",
191-
"electron-to-chromium",
192-
"node-releases",
193-
"baseline-browser-mapping"
194-
];
195-
const queryData = carriers.filter((carrier) =>
196-
Object.keys(require.cache).some((key) => key.includes(carrier))
197-
);
212+
const queryData = ${JSON.stringify(QUERY_DATA)}.filter((request) => {
213+
try {
214+
return require.cache[require.resolve(request)] !== undefined;
215+
} catch (_err) {
216+
return false;
217+
}
218+
});
198219
process.stdout.write(JSON.stringify({ target, queryData }));
199220
`;
200221
return JSON.parse(
201-
execFileSync(process.execPath, ["-e", script], { encoding: "utf8" })
222+
execFileSync(process.execPath, ["-e", script], {
223+
encoding: "utf8",
224+
env: environmentWithoutBrowserslist()
225+
})
202226
);
203227
};
204228

@@ -212,7 +236,7 @@ describe("browserslist target", () => {
212236
const { target, queryData } = resolveInChildProcess(withConfig);
213237
expect(target).toBe("browserslist");
214238
// A config means the queries do run, so the data is read after all
215-
expect(queryData).toContain("browserslist/index.js");
239+
expect(queryData).toEqual(QUERY_DATA);
216240
});
217241
});
218242
});

0 commit comments

Comments
 (0)