Unverified Commit 7b972a88 authored by Tobias Koppers's avatar Tobias Koppers Committed by GitHub

Fix `pnpm new-test` unit type generating no test file (#100641)

### What?

`pnpm new-test` with the `unit` type now generates a flat
`test/unit/<name>.test.ts` from a template, and nothing else.

### Why?

The `test` generator copied from `test/unit/test-template`, which didn't
exist. As a result, `pnpm new-test --args false my-unit unit` created no
test file at all, only `test/unit/my-unit/tsconfig.json` and
`next-env.d.ts`. That layout also doesn't match how unit tests are
organized: they are flat files in `test/unit/`. Since `AGENTS.md`
requires generating tests with `pnpm new-test`, the unit type needs to
work.

### How?

- Added a unit template, `test/unit/test-template/template.ts`. It's a
minimal Jest test that passes as generated, with a pointer on how to
import the code under test. A single `add` action renders it to
`test/unit/{{ toFileName name }}.test.ts`.
- Unlike the e2e templates, the file is deliberately not named
`*.test.ts` and has no spaces in its path. Otherwise the unit runner
would collect the template as a test, and on Windows `run-tests.js`
spawns jest through a shell that splits the Handlebars filename at its
spaces. That makes the `test unit windows` job pick up unrelated e2e
suites and fail.
- The generator treats unit tests as a separate case:
  - `appDir` is ignored, so output never goes into `test/unit/app-dir/`.
- The `tsconfig.json`/`next-env.d.ts` steps are skipped, since those
only make sense for Next.js app fixtures.
  - The e2e, production and development generation is unchanged.
- Documented the unit invocation in `AGENTS.md`.

### Verification

- `pnpm new-test --args false my-unit unit` and `pnpm new-test --args
true my-unit unit` both create only `test/unit/my-unit.test.ts`, and
`pnpm jest test/unit/my-unit.test.ts` passes.
- `node run-tests.js --type unit --dry` and `pnpm jest --listTests
test/unit/test-template` no longer pick up the template.
- `pnpm new-test --args true my-e2e e2e` and `pnpm new-test --args false
my-prod production` still generate their fixtures, test file,
`tsconfig.json` and `next-env.d.ts`.

<!-- NEXT_JS_LLM -->


<!-- fleet 813069b1-faf3-4c96-bd75-2be047fed0b3 -->

---------
Co-authored-by: 's avatarvercel-fleet-prod[bot] <318278635+vercel-fleet-prod[bot]@users.noreply.github.com>
Co-authored-by: 's avatarTobias Koppers <1365881+sokra@users.noreply.github.com>
parent 730f7bb1
...@@ -155,6 +155,9 @@ Generating tests using `pnpm new-test` is mandatory. ...@@ -155,6 +155,9 @@ Generating tests using `pnpm new-test` is mandatory.
# type: e2e | production | development | unit # type: e2e | production | development | unit
pnpm new-test --args true my-feature e2e pnpm new-test --args true my-feature e2e
# Unit tests are created as a flat test/unit/<name>.test.ts (appDir is ignored)
pnpm new-test --args false my-util unit
``` ```
**Analyzing test output efficiently:** **Analyzing test output efficiently:**
......
// Unit tests should test a specific utility without a browser or running
// `next`. Import the code under test from the compiled output, e.g.
// import { htmlEscapeJsonString } from 'next/dist/shared/lib/htmlescape'
describe('{{name}}', () => {
it('should work', () => {
expect(1 + 1).toBe(2)
})
})
...@@ -65,11 +65,26 @@ export default function generator(plop: NodePlopAPI): void { ...@@ -65,11 +65,26 @@ export default function generator(plop: NodePlopAPI): void {
const { appDir, type, name } = answers as TestResponse const { appDir, type, name } = answers as TestResponse
const basePath = plop.getDestBasePath() const basePath = plop.getDestBasePath()
const testRoot = path.join(basePath, 'test') const testRoot = path.join(basePath, 'test')
// Unit tests are single flat files in test/unit/ without a Next.js app
// fixture, so appDir doesn't apply. The template is deliberately not
// named `*.test.ts` (and has no spaces in its path) so the unit test
// runner doesn't pick it up as a test.
if (type === 'unit') {
return [
{
type: 'add',
templateFile: path.join(testRoot, 'unit/test-template/template.ts'),
path: path.join(testRoot, 'unit', '{{ toFileName name }}.test.ts'),
},
]
}
const appDirPath = appDir ? 'app-dir/' : '' const appDirPath = appDir ? 'app-dir/' : ''
const templatePath = path.join( const templatePath = path.join(
testRoot, testRoot,
type === 'unit' ? 'unit' : 'e2e', 'e2e',
appDirPath, appDirPath,
'test-template' 'test-template'
) )
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment