Skip to content

Commit

Permalink
Add browser test for graphql (#5263)
Browse files Browse the repository at this point in the history
  • Loading branch information
Timer authored Oct 3, 2018
1 parent 736561f commit b41e696
Show file tree
Hide file tree
Showing 13 changed files with 178 additions and 38 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`graphql with mjs entrypoint correctly bundles files in development 1`] = `"Pikachu"`;

exports[`graphql with mjs entrypoint correctly bundles files in production 1`] = `"Pikachu"`;
56 changes: 56 additions & 0 deletions fixtures/browser/graphql-with-mjs/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
const {
bootstrap,
startDevelopmentServer,
startProductionServer,
} = require('../../utils');
const puppeteer = require('puppeteer');

beforeEach(async () => {
await bootstrap({ directory: global.testDirectory, template: __dirname });
global.appDevPort = await startDevelopmentServer({
directory: global.testDirectory,
});
global.appProdPort = await startProductionServer({
directory: global.testDirectory,
});
// Wait for serve to boot up
await new Promise(resolve => setTimeout(resolve, 1000));
});

// https://github.com/facebook/create-react-app/issues/5234
// https://github.com/facebook/create-react-app/pull/5258
describe('graphql with mjs entrypoint', () => {
it('correctly bundles files in development', async () => {
const browser = await puppeteer.launch({ headless: true });
try {
const page = await browser.newPage();
await page.goto(`http://localhost:${global.appDevPort}/`);
await page.waitForSelector('.Pokemon-Name-Data');
const output = await page.evaluate(() => {
return Array.from(
document.getElementsByClassName('Pokemon-Name-Data')
).pop().innerHTML;
});
expect(output).toMatchSnapshot();
} finally {
browser.close();
}
});

it('correctly bundles files in production', async () => {
const browser = await puppeteer.launch({ headless: true });
try {
const page = await browser.newPage();
await page.goto(`http://localhost:${global.appProdPort}/`);
await page.waitForSelector('.Pokemon-Name-Data');
const output = await page.evaluate(() => {
return Array.from(
document.getElementsByClassName('Pokemon-Name-Data')
).pop().innerHTML;
});
expect(output).toMatchSnapshot();
} finally {
browser.close();
}
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"graphql": "14.0.2",
"react-apollo": "2.2.1",
"react": "latest",
"react-dom": "latest"
"react-dom": "latest",
"serve": "10.0.2"
}
}
63 changes: 63 additions & 0 deletions fixtures/browser/graphql-with-mjs/src/App.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import React, { Component } from 'react';
import ApolloClient, { gql } from 'apollo-boost';
import { ApolloProvider, Query } from 'react-apollo';

const GET_PIKA = gql`
{
pokemon(name: "Pikachu") {
name
}
}
`;

const client = new ApolloClient({
uri: 'https://graphql-pokemon.now.sh/graphql',
});

class Pokemon extends Component {
render() {
const { name } = this.props.pokemon;
return (
<h1>
Pokemon name: <span className="Pokemon-Name-Data">{name}</span>
</h1>
);
}
}

class Data extends Component {
state = {};
componentDidCatch() {
this.setState({ hasError: true });
}
render() {
const { hasError } = this.state;
return hasError ? (
<div className="Pokemon-Name-Data">Error :(</div>
) : (
<Query query={GET_PIKA}>
{({ loading, error, data }) => {
if (loading) {
return <div>Loading...</div>;
}
if (error) {
return <div className="Pokemon-Name-Data">Error :(</div>;
}
return <Pokemon pokemon={data.pokemon} />;
}}
</Query>
);
}
}

class App extends Component {
render() {
return (
<ApolloProvider client={client}>
<Data />
</ApolloProvider>
);
}
}

export default App;
7 changes: 7 additions & 0 deletions fixtures/browser/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = {
testEnvironment: 'node',
testMatch: ['**/*.test.js'],
testPathIgnorePatterns: ['/src/', 'node_modules'],
setupTestFrameworkScriptFile: './setupBrowserTests.js',
forceExit: true,
};
9 changes: 9 additions & 0 deletions fixtures/browser/setupBrowserTests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const fs = require('fs-extra');
const tempy = require('tempy');
beforeEach(() => {
global.testDirectory = tempy.directory();
jest.setTimeout(1000 * 60 * 5);
});
afterEach(() => {
fs.removeSync(global.testDirectory);
});
17 changes: 0 additions & 17 deletions fixtures/smoke/graphql-with-mjs/index.test.js

This file was deleted.

20 changes: 0 additions & 20 deletions fixtures/smoke/graphql-with-mjs/src/App.js

This file was deleted.

32 changes: 32 additions & 0 deletions fixtures/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,11 +135,43 @@ async function getOutputProduction({ directory, env = {} }) {
}
}

async function startDevelopmentServer({ directory, env = {} }) {
const port = await getPort();
execa('./node_modules/.bin/react-scripts', ['start'], {
cwd: directory,
env: Object.assign(
{},
{
BROWSER: 'none',
PORT: port,
CI: 'false',
FORCE_COLOR: '0',
},
env
),
});
return port;
}

async function startProductionServer({ directory, env = {} }) {
const port = await getPort();
await execa('./node_modules/.bin/react-scripts', ['build'], {
cwd: directory,
env: Object.assign({}, { CI: 'false' }, env),
});
execa('./node_modules/.bin/serve', ['-s', 'build', '-p', port], {
cwd: directory,
});
return port;
}

module.exports = {
bootstrap,
isSuccessfulDevelopment,
isSuccessfulProduction,
isSuccessfulTest,
getOutputDevelopment,
getOutputProduction,
startDevelopmentServer,
startProductionServer,
};
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"meow": "^5.0.0",
"multimatch": "^2.1.0",
"prettier": "1.14.3",
"puppeteer": "^1.8.0",
"strip-ansi": "^4.0.0",
"svg-term-cli": "^2.1.1",
"tempy": "^0.2.1"
Expand Down
3 changes: 3 additions & 0 deletions tasks/e2e-behavior.sh
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ git clean -df
# Now that we have published them, run all tests as if they were released.
# ******************************************************************************

# Browser tests
CI=true ./node_modules/.bin/jest --config fixtures/browser/jest.config.js

# Smoke tests
CI=true ./node_modules/.bin/jest --config fixtures/smoke/jest.config.js

Expand Down

0 comments on commit b41e696

Please sign in to comment.