Skip to content

Commit

Permalink
Feat/47/configure vite and workerd for tests (#53)
Browse files Browse the repository at this point in the history
* 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
nacho-vazquez authored Jul 6, 2024
1 parent b541f38 commit 63b933a
Show file tree
Hide file tree
Showing 26 changed files with 334 additions and 310 deletions.
7 changes: 6 additions & 1 deletion jest.preset.js
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'],
};
6 changes: 6 additions & 0 deletions nx.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@
"devTargetName": "dev",
"startTargetName": "start"
}
},
{
"plugin": "@nx/jest/plugin",
"options": {
"targetName": "test"
}
}
],
"generators": {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"devDependencies": {
"@angular-devkit/core": "~17.3.7",
"@cloudflare/next-on-pages": "^1.12.0",
"@cloudflare/vitest-pool-workers": "^0.4.7",
"@cloudflare/workers-types": "^4.20240512.0",
"@commitlint/cli": "^17.8.1",
"@commitlint/config-conventional": "^17.8.1",
Expand Down
11 changes: 0 additions & 11 deletions packages/e2e-utils/jest.config.ts

This file was deleted.

10 changes: 1 addition & 9 deletions packages/e2e-utils/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,5 @@
"sourceRoot": "packages/e2e-utils/src",
"projectType": "library",
"tags": [],
"targets": {
"test": {
"executor": "@nx/jest:jest",
"outputs": ["{workspaceRoot}/coverage/{projectRoot}"],
"options": {
"jestConfig": "packages/e2e-utils/jest.config.ts"
}
}
}
"targets": {}
}
3 changes: 3 additions & 0 deletions packages/nx-cloudflare-e2e/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
"sourceRoot": "packages/nx-cloudflare-e2e/src",
"implicitDependencies": ["nx-cloudflare"],
"targets": {
"test": {
"command": "echo 'Use e2e instead'"
},
"e2e": {
"executor": "@nx/jest:jest",
"outputs": ["{workspaceRoot}/coverage/{projectRoot}"],
Expand Down
32 changes: 16 additions & 16 deletions packages/nx-cloudflare/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,22 +155,22 @@ nx g @naxodev/nx-cloudflare:library my-worker-lib

Available options:

| Option | Type | Default | Description |
| ------------------------ | ------------------------------------- | ----------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| name | string | \*required | What name would you like to use? |
| directory | string | null | The directory of the new application. |
| projectNameAndRootFormat | as-provided, derived | as-provided | Whether to generate the project name and root directory as provided (`as-provided`) or generate them composing their values and taking the configured layout into account (`derived`). |
| linter | eslint, none | eslint | The tool to use for running lint checks. |
| unitTestRunner | vitest, none | vitest | Test runner to use for unit tests. |
| tags | string | null | Add tags to the application (used for linting). |
| skipFormat | boolean | false | Skip formatting files. |
| js | boolean | false | Use JavaScript instead of TypeScript |
| strict | boolean | true | Whether to enable tsconfig strict mode or not. |
| publishable | boolean | false | Generate a publishable library. |
| importPath | string | null | The library name used to import it, like @myorg/my-awesome-lib. Required for publishable library. |
| bundler | swc, tsc, rollup, vite, esbuild, none | tsc | Which bundler would you like to use to build the library? Choose 'none' to skip build setup. |
| minimal | boolean | false | Generate a library with a minimal setup. No README.md generated. |
| simpleName | boolean | false | Don't include the directory in the generated file name. |
| Option | Type | Default | Description |
| ------------------------ | ----------------------------- | ----------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| name | string | \*required | What name would you like to use? |
| directory | string | null | The directory of the new application. |
| projectNameAndRootFormat | as-provided, derived | as-provided | Whether to generate the project name and root directory as provided (`as-provided`) or generate them composing their values and taking the configured layout into account (`derived`). |
| linter | eslint, none | eslint | The tool to use for running lint checks. |
| unitTestRunner | vitest, none | vitest | Test runner to use for unit tests. |
| tags | string | null | Add tags to the application (used for linting). |
| skipFormat | boolean | false | Skip formatting files. |
| js | boolean | false | Use JavaScript instead of TypeScript |
| strict | boolean | true | Whether to enable tsconfig strict mode or not. |
| publishable | boolean | false | Generate a publishable library. |
| importPath | string | null | The library name used to import it, like @myorg/my-awesome-lib. Required for publishable library. |
| bundler | swc, tsc, vite, esbuild, none | tsc | Which bundler would you like to use to build the library? Choose 'none' to skip build setup. |
| minimal | boolean | false | Generate a library with a minimal setup. No README.md generated. |
| simpleName | boolean | false | Don't include the directory in the generated file name. |

### NextJS on Cloudflare

Expand Down
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 %>

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!"`);
});
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!"`);
});
});
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!"`);
});
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!"`);
});
});
Loading

0 comments on commit 63b933a

Please sign in to comment.