forked from JS-DevTools/npm-publish
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuse-npm-environment.test.ts
80 lines (70 loc) · 2.6 KB
/
use-npm-environment.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import fs from "node:fs/promises";
import os from "node:os";
import path from "node:path";
import { describe, beforeEach, afterEach, it, expect } from "vitest";
import type { PackageManifest } from "../../read-manifest.js";
import type { NormalizedOptions } from "../../normalize-options.js";
import * as subject from "../use-npm-environment.js";
describe("useNpmEnvironment", () => {
let directory: string;
beforeEach(async () => {
directory = await fs.mkdtemp(path.join(os.tmpdir(), "read-manifest-test-"));
});
afterEach(async () => {
await fs.rm(directory, { recursive: true, force: true });
});
it.each([
{
registryUrl: "http://example.com/",
expectedAuthConfig: "//example.com/:_authToken=${NODE_AUTH_TOKEN}",
expectedRegistryConfig: "registry=http://example.com/",
},
{
registryUrl: "http://example.com",
expectedAuthConfig: "//example.com/:_authToken=${NODE_AUTH_TOKEN}",
expectedRegistryConfig: "registry=http://example.com/",
},
{
registryUrl: "http://example.com/hello/",
expectedAuthConfig: "//example.com/hello/:_authToken=${NODE_AUTH_TOKEN}",
expectedRegistryConfig: "registry=http://example.com/hello/",
},
{
registryUrl: "http://example.com/hello",
expectedAuthConfig: "//example.com/hello/:_authToken=${NODE_AUTH_TOKEN}",
expectedRegistryConfig: "registry=http://example.com/hello/",
},
])(
"creates an npmrc file for $registryUrl",
async ({ registryUrl, expectedAuthConfig, expectedRegistryConfig }) => {
const inputManifest = { name: "fizzbuzz" } as PackageManifest;
const inputOptions = {
token: "abc123",
registry: new URL(registryUrl),
temporaryDirectory: directory,
} as NormalizedOptions;
let npmrcPath: string | undefined;
let npmrcContents: string | undefined;
const result = await subject.useNpmEnvironment(
inputManifest,
inputOptions,
async (manifest, options, environment) => {
npmrcPath = environment["npm_config_userconfig"]!;
npmrcContents = await fs.readFile(npmrcPath, "utf8");
return { manifest, options, environment };
}
);
expect(result).toEqual({
manifest: inputManifest,
options: inputOptions,
environment: {
NODE_AUTH_TOKEN: "abc123",
npm_config_userconfig: npmrcPath,
},
});
expect(npmrcContents).toContain(expectedAuthConfig);
expect(npmrcContents).toContain(expectedRegistryConfig);
await expect(fs.access(npmrcPath!)).rejects.toThrow(/ENOENT/);
}
);
});