-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: improve generated tests by using Vitest and workerd (#53)
* feat: configure vitest-pool-workers * fix: remove unneded test script * feat: add needed vitest configuration * feat: add template for new unit tests and integration tests format * fix: nrwl/nx#23435
- Loading branch information
1 parent
b541f38
commit 54be56b
Showing
28 changed files
with
337 additions
and
311 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -1,3 +1,8 @@ | ||
const nxPreset = require('@nx/jest/preset').default; | ||
|
||
module.exports = { ...nxPreset }; | ||
module.exports = { | ||
...nxPreset, | ||
maxWorkers: 1, | ||
testEnvironment: 'node', | ||
setupFiles: ['../../tools/scripts/unit-test-setup.js'], | ||
}; |
This file contains 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
This file contains 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
This file was deleted.
Oops, something went wrong.
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
3 changes: 2 additions & 1 deletion
3
packages/nx-cloudflare/src/generators/application/files/common/wrangler.toml__tmpl__
This file contains 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 |
---|---|---|
@@ -1,5 +1,6 @@ | ||
name = "<%=name %>" | ||
compatibility_date = "2024-04-19" | ||
compatibility_date = "2024-01-01" | ||
compatibility_flags = ["nodejs_compat"] | ||
main = "src/index.<%=extension %>" | ||
<%-accountId %> | ||
|
9 changes: 9 additions & 0 deletions
9
...oudflare/src/generators/application/files/fetch-handler/index.integration.test.ts__tmpl__
This file contains 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,9 @@ | ||
import { SELF } from "cloudflare:test"; | ||
<%-vitestImports %> | ||
import "../src"; | ||
|
||
// an integration test using SELF | ||
it("sends request (integration style)", async () => { | ||
const response = await SELF.fetch("http://example.com"); | ||
expect(await response.text()).toMatchInlineSnapshot(`"Hello World!"`); | ||
}); |
26 changes: 7 additions & 19 deletions
26
packages/nx-cloudflare/src/generators/application/files/fetch-handler/index.test.ts__tmpl__
This file contains 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 |
---|---|---|
@@ -1,25 +1,13 @@ | ||
import { unstable_dev } from 'wrangler'; | ||
import type { UnstableDevWorker } from 'wrangler'; | ||
import { env, createExecutionContext, waitOnExecutionContext, } from "cloudflare:test"; | ||
import { worker } from './src' | ||
<%-vitestImports %> | ||
|
||
describe('Worker', () => { | ||
let worker: UnstableDevWorker; | ||
|
||
beforeAll(async () => { | ||
worker = await unstable_dev('src/index.ts', { | ||
experimental: { disableExperimentalWarning: true }, | ||
}); | ||
}); | ||
|
||
afterAll(async () => { | ||
await worker.stop(); | ||
}); | ||
|
||
it('should return Hello World', async () => { | ||
const resp = await worker.fetch(); | ||
if (resp) { | ||
const text = await resp.text(); | ||
expect(text).toMatchInlineSnapshot(`"Hello World!"`); | ||
} | ||
const request = new Request("http://example.com/"); | ||
const ctx = createExecutionContext(); | ||
const response = await worker.fetch(request, env, ctx); | ||
await waitOnExecutionContext(ctx); | ||
expect(await response.text()).toMatchInlineSnapshot(`"Hello World!"`); | ||
}); | ||
}); |
10 changes: 10 additions & 0 deletions
10
...lare/src/generators/application/files/scheduled-handler/index.integration.test.ts__tmpl__
This file contains 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,10 @@ | ||
|
||
import { SELF } from "cloudflare:test"; | ||
<%-vitestImports %> | ||
import "../src"; | ||
|
||
// an integration test using SELF | ||
it("sends request (integration style)", async () => { | ||
const response = await SELF.fetch("http://example.com"); | ||
expect(await response.text()).toMatchInlineSnapshot(`"Hello World!"`); | ||
}); |
26 changes: 7 additions & 19 deletions
26
...es/nx-cloudflare/src/generators/application/files/scheduled-handler/index.test.ts__tmpl__
This file contains 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 |
---|---|---|
@@ -1,25 +1,13 @@ | ||
import { unstable_dev } from 'wrangler'; | ||
import type { UnstableDevWorker } from 'wrangler'; | ||
import { env, createExecutionContext, waitOnExecutionContext, } from "cloudflare:test"; | ||
import { worker } from './src' | ||
<%-vitestImports %> | ||
|
||
describe('Worker', () => { | ||
let worker: UnstableDevWorker; | ||
|
||
beforeAll(async () => { | ||
worker = await unstable_dev('src/index.ts', { | ||
experimental: { disableExperimentalWarning: true }, | ||
}); | ||
}); | ||
|
||
afterAll(async () => { | ||
await worker.stop(); | ||
}); | ||
|
||
it('should return Hello World', async () => { | ||
const resp = await worker.fetch(); | ||
if (resp) { | ||
const text = await resp.text(); | ||
expect(text).toMatchInlineSnapshot(`"Hello World!"`); | ||
} | ||
const request = new Request("http://example.com/"); | ||
const ctx = createExecutionContext(); | ||
const response = await worker.fetch(request, env, ctx); | ||
await waitOnExecutionContext(ctx); | ||
expect(await response.text()).toMatchInlineSnapshot(`"Hello World!"`); | ||
}); | ||
}); |
Oops, something went wrong.