test: add more test

This commit is contained in:
zanminkian 2025-04-14 04:07:31 +00:00
parent 1435643a31
commit b6b489ab80
2 changed files with 47 additions and 0 deletions

View File

@ -1314,6 +1314,30 @@ it(`should download latest pnpm from custom registry`, async () => {
}); });
}); });
it(`should download from npm registry url by default`, async () => {
await xfs.mktempPromise(async cwd => {
process.env.npm_config_registry = `https://registry.npmmirror.com`;
process.env.COREPACK_ENABLE_DOWNLOAD_PROMPT = `1`;
await xfs.writeJsonPromise(ppath.join(cwd, `package.json` as Filename), {
packageManager: `pnpm@10.1.0`,
});
await expect(runCli(cwd, [`pnpm`, `--version`])).resolves.toMatchObject({
exitCode: 0,
stdout: `10.1.0\n`,
stderr: `! Corepack is about to download https://registry.npmmirror.com/pnpm/-/pnpm-10.1.0.tgz\n`,
});
// Should keep working with cache
await expect(runCli(cwd, [`pnpm`, `--version`])).resolves.toMatchObject({
exitCode: 0,
stdout: `10.1.0\n`,
stderr: ``,
});
});
});
describe(`should pick up COREPACK_INTEGRITY_KEYS from env`, () => { describe(`should pick up COREPACK_INTEGRITY_KEYS from env`, () => {
beforeEach(() => { beforeEach(() => {
process.env.AUTH_TYPE = `COREPACK_NPM_TOKEN`; // See `_registryServer.mjs` process.env.AUTH_TYPE = `COREPACK_NPM_TOKEN`; // See `_registryServer.mjs`

View File

@ -36,6 +36,15 @@ describe(`npm registry utils fetchAsJson`, () => {
expect(httpFetchAsJson).lastCalledWith(`${process.env.COREPACK_NPM_REGISTRY}/package-name`, {headers: DEFAULT_HEADERS}); expect(httpFetchAsJson).lastCalledWith(`${process.env.COREPACK_NPM_REGISTRY}/package-name`, {headers: DEFAULT_HEADERS});
}); });
it(`loads from npm registry url`, async () => {
// `process.env` is reset after each tests in setupTests.js.
process.env.npm_config_registry = `https://registry.example.org`;
await fetchAsJson(`package-name`);
expect(httpFetchAsJson).toBeCalled();
expect(httpFetchAsJson).lastCalledWith(`${process.env.npm_config_registry}/package-name`, {headers: DEFAULT_HEADERS});
});
it(`adds authorization header with bearer token if COREPACK_NPM_TOKEN is set`, async () => { it(`adds authorization header with bearer token if COREPACK_NPM_TOKEN is set`, async () => {
// `process.env` is reset after each tests in setupTests.js. // `process.env` is reset after each tests in setupTests.js.
process.env.COREPACK_NPM_TOKEN = `foo`; process.env.COREPACK_NPM_TOKEN = `foo`;
@ -50,6 +59,20 @@ describe(`npm registry utils fetchAsJson`, () => {
}}); }});
}); });
it(`uses npm registry url if COREPACK_NPM_TOKEN is set`, async () => {
// `process.env` is reset after each tests in setupTests.js.
process.env.COREPACK_NPM_TOKEN = `foo`;
process.env.npm_config_registry = `https://registry.example.org`;
await fetchAsJson(`package-name`);
expect(httpFetchAsJson).toBeCalled();
expect(httpFetchAsJson).lastCalledWith(`${process.env.npm_config_registry}/package-name`, {headers: {
...DEFAULT_HEADERS,
authorization: `Bearer ${process.env.COREPACK_NPM_TOKEN}`,
}});
});
it(`only adds authorization header with bearer token if COREPACK_NPM_TOKEN and COREPACK_NPM_USERNAME are set`, async () => { it(`only adds authorization header with bearer token if COREPACK_NPM_TOKEN and COREPACK_NPM_USERNAME are set`, async () => {
// `process.env` is reset after each tests in setupTests.js. // `process.env` is reset after each tests in setupTests.js.
process.env.COREPACK_NPM_TOKEN = `foo`; process.env.COREPACK_NPM_TOKEN = `foo`;