Skip to content
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

chore: switch existing tests to new next-test-helper library #629

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@
"gulp-notify": "2.2.0",
"husky": "0.12.0",
"jest": "^18.0.0",
"next-test-helper": "^1.0.6",
"nyc": "^10.0.0",
"run-sequence": "1.2.2",
"standard": "8.6.0",
Expand Down
56 changes: 25 additions & 31 deletions test/integration.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,71 +3,69 @@
'use strict'

import { join } from 'path'
import next from '../dist/server/next'
import pkg from '../package.json'
import next from '../dist/server/next'
import {expectElement, setup, render, teardown} from 'next-test-helper'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I hope we need to expose some low level helpers rather these generic helpers.
For an example, here are few cases:

  • Get the programmatic API
  • Run an app by giving the path
  • Get a random available port

There are many ways, we could write tests. It's depend on test case to case.
Specially with Next.js integration tests, it gets so crazy. Sometimes, we may need to do npm install.


let app = null
const dir = join(__dirname, 'fixtures', 'basic')
const app = next({
dir,
dev: true,
staticMarkup: true,
quiet: true
})

jasmine.DEFAULT_TIMEOUT_INTERVAL = 20000

describe('integration tests', () => {
beforeAll(() => app.prepare())

afterAll(() => app.close())
beforeAll(async () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

beforeAll(() => setup(dir, next)) ?

app = await setup(dir, next)
})
afterAll(async () => await teardown())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

afterAll(teardown) ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 wasn't sure if Jest was smart enough to detect that the teardown was async/await I'll give this a try.


test('renders a stateless component', async () => {
const html = await render('/stateless')
expect(html.includes('<meta charset="utf-8" class="next-head"/>')).toBeTruthy()
expect(html.includes('<h1>My component!</h1>')).toBeTruthy()
const element = await render('/stateless')
expect(element.find('meta').attr('class')).toMatch('next-head')
expectElement(element.find('h1')).to.have.text('My component!')
})

test('renders a stateful component', async () => {
const html = await render('/stateful')
expect(html.includes('<div><p>The answer is 42</p></div>')).toBeTruthy()
const element = await render('/stateful')
expectElement(element.find('div p')).to.have.text('The answer is 42')
})

test('header helper renders header information', async () => {
const html = await (render('/head'))
expect(html.includes('<meta charset="iso-8859-5" class="next-head"/>')).toBeTruthy()
expect(html.includes('<meta content="my meta" class="next-head"/>')).toBeTruthy()
expect(html.includes('<div><h1>I can haz meta tags</h1></div>')).toBeTruthy()
const element = await (render('/head'))
expect(element.find('meta[charset=iso-8859-5]').attr('class')).toMatch('next-head')
expect(element.find('meta[content="my meta"]').attr('class')).toMatch('next-head')
expectElement(element.find('div h1')).to.have.text('I can haz meta tags')
})

test('css helper renders styles', async () => {
const html = await render('/css')
const element = await render('/css')
const html = element.toString()
expect(/\.css-\w+/.test(html)).toBeTruthy()
expect(/<div class="css-\w+">This is red<\/div>/.test(html)).toBeTruthy()
})

test('renders styled jsx', async () => {
const html = await render('/styled-jsx')
const html = (await render('/styled-jsx')).toString()
expect(html).toMatch(/<style id="__jsx-style-1401785258">p\[data-jsx="1401785258"] {color: blue }[^]+<\/style>/)
expect(html.includes('<div data-jsx="1401785258"><p data-jsx="1401785258">This is blue</p></div>')).toBeTruthy()
})

test('renders properties populated asynchronously', async () => {
const html = await render('/async-props')
expect(html.includes('<p>Diego Milito</p>')).toBeTruthy()
const element = await render('/async-props')
expectElement(element.find('p')).to.have.text('Diego Milito')
})

test('renders a link component', async () => {
const html = await render('/link')
const html = (await render('/link')).toString()
expect(html.includes('<a href="/about">About</a>')).toBeTruthy()
})

test('error', async () => {
const html = await render('/error')
const html = (await render('/error')).toString()
expect(html).toMatch(/<pre class=".+">Error: This is an expected error\n[^]+<\/pre>/)
})

test('error 404', async () => {
const html = await render('/non-existent')
const html = (await render('/non-existent')).toString()
expect(html).toMatch(/<h1 data-jsx=".+">404<\/h1>/)
expect(html).toMatch(/<h2 data-jsx=".+">This page could not be found\.<\/h2>/)
})
Expand All @@ -79,7 +77,7 @@ describe('integration tests', () => {
this.finished = true
}
}
const html = await app.renderToHTML({}, res, '/finish-response', {})
const html = (await render('/finish-response', {}, {}, res)).toString()
expect(html).toBeFalsy()
})

Expand Down Expand Up @@ -116,7 +114,3 @@ describe('integration tests', () => {
})
})
})

function render (pathname, query = {}) {
return app.renderToHTML({}, {}, pathname, query)
}