Update pnpm new-test to use createNextDescribe (#44147)

This commit is contained in:
Tim Neutkens 2022-12-19 20:08:50 +01:00 committed by GitHub
parent 60b8468b35
commit 397fc57224
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 51 additions and 27 deletions

View file

@ -6,6 +6,12 @@ module.exports = function (plop) {
plop.setGenerator('test', { plop.setGenerator('test', {
description: 'Create a new test', description: 'Create a new test',
prompts: [ prompts: [
{
type: 'confirm',
name: 'appDir',
message: 'Is this test for the app directory?',
default: false,
},
{ {
type: 'input', type: 'input',
name: 'name', name: 'name',
@ -38,10 +44,17 @@ module.exports = function (plop) {
data.type === 'unit' ? 'unit' : 'e2e' data.type === 'unit' ? 'unit' : 'e2e'
}/example.txt`, }/example.txt`,
path: `test/{{type}}/${ path: `test/{{type}}/${
data.type === 'unit' data.appDir ? 'app-dir/' : ''
? `${fileName}.test.ts` }${fileName}/${fileName}.test.ts`,
: `${fileName}/index.test.ts` },
}`, {
type: 'add',
templateFile: `test/${
data.type === 'unit' ? 'unit' : 'e2e'
}/example-file.txt`,
path: `test/{{type}}/${
data.appDir ? 'app-dir/' : ''
}${fileName}/pages/index.js`,
}, },
] ]
}, },

View file

@ -0,0 +1,3 @@
export default function Page() {
return <p>hello world</p>
}

View file

@ -1,26 +1,34 @@
import { createNext } from 'e2e-utils' import { createNextDescribe } from 'e2e-utils'
import { NextInstance } from 'test/lib/next-modes/base'
import { renderViaHTTP } from 'next-test-utils'
describe('{{name}}', () => { createNextDescribe(
let next: NextInstance '{{name}}',
{
beforeAll(async () => { files: __dirname,
next = await createNext({ },
files: { ({ next }) => {
'pages/index.js': ` // Recommended for tests that check HTML. Cheerio is a HTML parser that has a jQuery like API.
export default function Page() { it('should work using cheerio', async () => {
return <p>hello world</p> const $ = await next.render$('/')
} expect($('p').text()).toBe('hello world')
`
},
dependencies: {}
}) })
})
afterAll(() => next.destroy())
it('should work', async () => { // Recommended for tests that need a full browser
const html = await renderViaHTTP(next.url, '/') it('should work using browser', async () => {
expect(html).toContain('hello world') const browser = await next.browser('/')
}) expect(await browser.getElementByCss('p').text()).toBe('hello world')
}) })
// In case you need the full HTML. Can also use $.html() with cheerio.
it('should work with html', async () => {
const html = await next.render('/')
expect(html).toContain('hello world')
})
// In case you need to test the response object
it('should work with fetch', async () => {
const res = await next.fetch('/')
const html = await res.text()
expect(html).toContain('hello world')
})
}
)