Skip to content

Commit 67c6634

Browse files
Various workers-utils improvements/fixes (#11460)
* Add missing `test` script in workers-utils package * Move parse tests from wrangler to workers-utils (since they are testing workers-utils functions) * add `vitest/globals` to workers-utils tests tsconfig * add missing `type:tests` script to workers-utils
1 parent 8e8ab6f commit 67c6634

File tree

4 files changed

+83
-81
lines changed

4 files changed

+83
-81
lines changed

packages/workers-utils/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@
3232
"check:lint": "eslint . --max-warnings=0",
3333
"check:type": "tsc -p ./tsconfig.json",
3434
"dev": "concurrently -c black,blue --kill-others-on-fail false \"pnpm tsup --watch src\" \"pnpm run check:type --watch --preserveWatchOutput\"",
35-
"test:ci": "vitest run"
35+
"test": "vitest",
36+
"test:ci": "vitest run",
37+
"type:tests": "tsc -p ./tests/tsconfig.json"
3638
},
3739
"devDependencies": {
3840
"@cloudflare/containers-shared": "workspace:*",

packages/wrangler/src/__tests__/parse.test.ts renamed to packages/workers-utils/tests/parse.test.ts

Lines changed: 1 addition & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -5,85 +5,7 @@ import {
55
parseJSONC,
66
parseTOML,
77
searchLocation,
8-
} from "@cloudflare/workers-utils";
9-
import { formatMessage } from "../utils/format-message";
10-
import type { Message } from "@cloudflare/workers-utils";
11-
12-
describe("formatMessage", () => {
13-
const format = (input: Message) => {
14-
// No color and skip emojis at the start.
15-
return formatMessage(input, false).substring(2);
16-
};
17-
18-
it("should format message without location", () => {
19-
expect(
20-
format({
21-
text: "Invalid argument",
22-
kind: "warning",
23-
})
24-
).toMatchInlineSnapshot(`
25-
"[WARNING] Invalid argument
26-
27-
"
28-
`);
29-
});
30-
31-
it("should format message with location", () => {
32-
expect(
33-
format({
34-
text: "Missing property: main",
35-
location: {
36-
line: 1,
37-
column: 0,
38-
lineText: "{}",
39-
file: "package.json",
40-
fileText: "{}",
41-
},
42-
})
43-
).toMatchInlineSnapshot(`
44-
"[ERROR] Missing property: main
45-
46-
package.json:1:0:
47-
1 │ {}
48-
╵ ^
49-
50-
"
51-
`);
52-
});
53-
54-
it("should format message with location and notes", () => {
55-
expect(
56-
format({
57-
text: "Invalid property: type",
58-
location: {
59-
line: 3,
60-
column: 8,
61-
length: 7,
62-
lineText: "type = 'modular'",
63-
suggestion: "Did you mean 'module'?",
64-
file: "package.toml",
65-
fileText: "[package]\ntype = 'modular'\n",
66-
},
67-
notes: [
68-
{
69-
text: "There are two acceptable types: 'module' and 'commonjs'",
70-
},
71-
],
72-
})
73-
).toMatchInlineSnapshot(`
74-
"[ERROR] Invalid property: type
75-
76-
package.toml:3:8:
77-
3 │ type = 'modular'
78-
│ ~~~~~~~
79-
╵ Did you mean 'module'?
80-
81-
There are two acceptable types: 'module' and 'commonjs'
82-
83-
"
84-
`);
85-
});
86-
});
8+
} from "../src/parse";
879

8810
describe("parseTOML", () => {
8911
it("should parse toml that is empty", () => {

packages/workers-utils/tests/tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"extends": "@cloudflare/workers-tsconfig/tsconfig.json",
33
"compilerOptions": {
44
"module": "preserve",
5-
"types": ["node"],
5+
"types": ["node", "vitest/globals"],
66
"jsx": "preserve"
77
},
88
"include": ["../*.d.ts", "**/*.ts", "**/*.tsx", "**/*.js"]
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import { formatMessage } from "../../utils/format-message";
2+
import type { Message } from "@cloudflare/workers-utils";
3+
4+
describe("formatMessage", () => {
5+
const format = (input: Message) => {
6+
// No color and skip emojis at the start.
7+
return formatMessage(input, false).substring(2);
8+
};
9+
10+
it("should format message without location", () => {
11+
expect(
12+
format({
13+
text: "Invalid argument",
14+
kind: "warning",
15+
})
16+
).toMatchInlineSnapshot(`
17+
"[WARNING] Invalid argument
18+
19+
"
20+
`);
21+
});
22+
23+
it("should format message with location", () => {
24+
expect(
25+
format({
26+
text: "Missing property: main",
27+
location: {
28+
line: 1,
29+
column: 0,
30+
lineText: "{}",
31+
file: "package.json",
32+
fileText: "{}",
33+
},
34+
})
35+
).toMatchInlineSnapshot(`
36+
"[ERROR] Missing property: main
37+
38+
package.json:1:0:
39+
1 │ {}
40+
╵ ^
41+
42+
"
43+
`);
44+
});
45+
46+
it("should format message with location and notes", () => {
47+
expect(
48+
format({
49+
text: "Invalid property: type",
50+
location: {
51+
line: 3,
52+
column: 8,
53+
length: 7,
54+
lineText: "type = 'modular'",
55+
suggestion: "Did you mean 'module'?",
56+
file: "package.toml",
57+
fileText: "[package]\ntype = 'modular'\n",
58+
},
59+
notes: [
60+
{
61+
text: "There are two acceptable types: 'module' and 'commonjs'",
62+
},
63+
],
64+
})
65+
).toMatchInlineSnapshot(`
66+
"[ERROR] Invalid property: type
67+
68+
package.toml:3:8:
69+
3 │ type = 'modular'
70+
│ ~~~~~~~
71+
╵ Did you mean 'module'?
72+
73+
There are two acceptable types: 'module' and 'commonjs'
74+
75+
"
76+
`);
77+
});
78+
});

0 commit comments

Comments
 (0)