-
Notifications
You must be signed in to change notification settings - Fork 73
test: add unit test for the optional dependency patch #277
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from 3 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
a07e68a
test: add unit test for the optional dependency patch
vicb a930893
fixup! rename + type
vicb a931fd8
fixup!
vicb 5f24884
Update packages/cloudflare/src/cli/build/patches/ast/util.ts
vicb cafb654
Update packages/cloudflare/src/cli/build/patches/ast/util.ts
vicb e8b2fc6
Update packages/cloudflare/src/cli/build/patches/ast/util.ts
vicb 97a4619
Update packages/cloudflare/src/cli/build/patches/ast/util.ts
vicb File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
101 changes: 101 additions & 0 deletions
101
packages/cloudflare/src/cli/build/patches/ast/optional-deps.spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
import { describe, expect, it } from "vitest"; | ||
|
||
import { optionalDepRule } from "./optional-deps.js"; | ||
import { patchCode } from "./util.js"; | ||
|
||
describe("optional dependecy", () => { | ||
it('should wrap a top-level require("caniuse-lite") in a try-catch', () => { | ||
const code = `t = require("caniuse-lite");`; | ||
expect(patchCode(code, optionalDepRule)).toMatchInlineSnapshot(` | ||
"try { | ||
t = require("caniuse-lite"); | ||
} catch { | ||
throw new Error('The optional dependency "caniuse-lite" is not installed'); | ||
};" | ||
`); | ||
}); | ||
|
||
it('should wrap a top-level require("caniuse-lite/data") in a try-catch', () => { | ||
const code = `t = require("caniuse-lite/data");`; | ||
expect(patchCode(code, optionalDepRule)).toMatchInlineSnapshot( | ||
` | ||
"try { | ||
t = require("caniuse-lite/data"); | ||
} catch { | ||
throw new Error('The optional dependency "caniuse-lite/data" is not installed'); | ||
};" | ||
` | ||
); | ||
}); | ||
|
||
it('should wrap e.exports = require("caniuse-lite") in a try-catch', () => { | ||
const code = 'e.exports = require("caniuse-lite");'; | ||
expect(patchCode(code, optionalDepRule)).toMatchInlineSnapshot(` | ||
"try { | ||
e.exports = require("caniuse-lite"); | ||
} catch { | ||
throw new Error('The optional dependency "caniuse-lite" is not installed'); | ||
};" | ||
`); | ||
}); | ||
|
||
it('should wrap module.exports = require("caniuse-lite") in a try-catch', () => { | ||
const code = 'module.exports = require("caniuse-lite");'; | ||
expect(patchCode(code, optionalDepRule)).toMatchInlineSnapshot(` | ||
"try { | ||
module.exports = require("caniuse-lite"); | ||
} catch { | ||
throw new Error('The optional dependency "caniuse-lite" is not installed'); | ||
};" | ||
`); | ||
}); | ||
|
||
it('should wrap exports.foo = require("caniuse-lite") in a try-catch', () => { | ||
const code = 'exports.foo = require("caniuse-lite");'; | ||
expect(patchCode(code, optionalDepRule)).toMatchInlineSnapshot(` | ||
"try { | ||
exports.foo = require("caniuse-lite"); | ||
} catch { | ||
throw new Error('The optional dependency "caniuse-lite" is not installed'); | ||
};" | ||
`); | ||
}); | ||
|
||
it('should not wrap require("lodash") in a try-catch', () => { | ||
const code = 't = require("lodash");'; | ||
expect(patchCode(code, optionalDepRule)).toMatchInlineSnapshot(`"t = require("lodash");"`); | ||
}); | ||
|
||
it('should not wrap require("other-module") if it does not match caniuse-lite regex', () => { | ||
const code = 't = require("other-module");'; | ||
expect(patchCode(code, optionalDepRule)).toMatchInlineSnapshot(`"t = require("other-module");"`); | ||
}); | ||
|
||
it("should not wrap a require() call already inside a try-catch", () => { | ||
const code = ` | ||
try { | ||
const t = require("caniuse-lite"); | ||
} catch {} | ||
`; | ||
expect(patchCode(code, optionalDepRule)).toMatchInlineSnapshot(` | ||
"try { | ||
const t = require("caniuse-lite"); | ||
} catch {} | ||
" | ||
`); | ||
}); | ||
|
||
it("should handle require with subpath and not wrap if already in try-catch", () => { | ||
const code = ` | ||
try { | ||
const t = require("caniuse-lite/path"); | ||
} catch {} | ||
`; | ||
expect(patchCode(code, optionalDepRule)).toMatchInlineSnapshot(` | ||
"try { | ||
const t = require("caniuse-lite/path"); | ||
} catch {} | ||
" | ||
`); | ||
}); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
packages/cloudflare/src/cli/build/patches/ast/util.spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { afterEach, describe, expect, it, vi } from "vitest"; | ||
|
||
import { patchCode } from "./util.js"; | ||
|
||
describe("patchCode", () => { | ||
afterEach(() => { | ||
vi.clearAllMocks(); | ||
}); | ||
|
||
it("should throw an error if rule has a transform", () => { | ||
expect(() => | ||
patchCode(`console.log("hi")`, { rule: { pattern: "console.log($MSG)" }, transform: "not supported" }) | ||
).toThrow(/not supported/); | ||
}); | ||
|
||
it("should throw an error if rule has no fix", () => { | ||
expect(() => patchCode(`console.log("hi")`, { rule: { pattern: "console.log($MSG)" } })).toThrow( | ||
/no fix/ | ||
); | ||
}); | ||
|
||
it("should accept yaml rules", () => { | ||
const yamlRule = ` | ||
rule: | ||
pattern: a | ||
fix: b | ||
`; | ||
|
||
expect(patchCode(`a`, yamlRule)).toEqual("b"); | ||
}); | ||
|
||
it("should apply fix to a single match when once is true", () => { | ||
expect(patchCode(`a+a`, { rule: { pattern: "a" }, fix: "b" }, { once: true })).toEqual("b+a"); | ||
}); | ||
|
||
it("should apply fix to all matches when once is false (default)", () => { | ||
expect(patchCode(`a+a`, { rule: { pattern: "a" }, fix: "b" })).toEqual("b+b"); | ||
expect(patchCode(`a+a`, { rule: { pattern: "a" }, fix: "b" }, { once: false })).toEqual("b+b"); | ||
}); | ||
|
||
it("should handle no matches", () => { | ||
expect(patchCode(`a`, { rule: { pattern: "b" }, fix: "c" })).toEqual("a"); | ||
}); | ||
|
||
it("should replace $PLACEHOLDER with match text", () => { | ||
expect( | ||
patchCode(`console.log(message)`, { rule: { pattern: "console.log($MSG)" }, fix: "$MSG" }) | ||
).toEqual("message"); | ||
}); | ||
|
||
it("should handle $PLACEHODLERS that are not found in matches", () => { | ||
expect( | ||
patchCode(`console.log(message)`, { rule: { pattern: "console.log($MSG)" }, fix: "$WHAT$$$WHAT" }) | ||
).toEqual("$WHAT"); | ||
}); | ||
|
||
it("should replace $$$PLACEHOLDER with match text", () => { | ||
expect( | ||
patchCode(`console.log("hello" + world, "!")`, { | ||
rule: { pattern: "console.log($$$ARGS)" }, | ||
fix: "$$$ARGS", | ||
}) | ||
).toEqual(`"hello" + world,"!"`); | ||
}); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.