Skip to content

Commit

Permalink
add test to run loader simultaneously
Browse files Browse the repository at this point in the history
Ref. #7
  • Loading branch information
piglovesyou committed Jan 10, 2020
1 parent 19aec11 commit b3dbc90
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 18 deletions.
7 changes: 5 additions & 2 deletions test/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ import webpack from 'webpack';
import memoryfs from 'memory-fs';
import nodeExternals from 'webpack-node-externals';

export default (fixture: string): Promise<webpack.Stats> => {
export default (
fixture: string,
target: 'node' | 'web',
): Promise<webpack.Stats> => {
const compiler = webpack({
mode: 'production',
context: __dirname,
Expand All @@ -12,7 +15,7 @@ export default (fixture: string): Promise<webpack.Stats> => {
path: path.resolve(__dirname),
filename: 'bundle.js',
},
target: 'node',
target,
externals: [nodeExternals()],
module: {
rules: [
Expand Down
55 changes: 39 additions & 16 deletions test/loader.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,7 @@
import assert from 'assert';
import compiler from './compiler';

test(
'Inserts name and outputs JavaScript',
async () => {
const fixture = 'pages/viewer.graphql';
const expect = `function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
const expect = `function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
import gql from 'graphql-tag';
import * as React from 'react';
Expand Down Expand Up @@ -48,14 +44,41 @@ export function useViewerLazyQuery(baseOptions) {
return ApolloReactHooks.useLazyQuery(ViewerDocument, baseOptions);
}`;

const stats = await compiler(fixture);
const { 0: actual, length } = stats
.toJson()
.modules!.map(m => m.source)
.filter(Boolean);

assert.deepStrictEqual(length, 1);
assert.deepStrictEqual(actual, expect);
},
60 * 1000,
);
describe('graphql-let/loader', () => {
test(
'generates .tsx and .d.ts',
async () => {
const fixture = 'pages/viewer.graphql';
const stats = await compiler(fixture, 'node');
const { 0: actual, length } = stats
.toJson()
.modules!.map(m => m.source)
.filter(Boolean);

assert.deepStrictEqual(length, 1);
assert.deepStrictEqual(actual, expect);
},
60 * 1000,
);

test(
'runs well for simultaneous execution assuming SSR',
async () => {
const fixture = 'pages/viewer.graphql';
const results = await Promise.all([
compiler(fixture, 'node'),
compiler(fixture, 'web'),
]);
for (const stats of results) {
const { 0: actual, length } = stats
.toJson()
.modules!.map(m => m.source)
.filter(Boolean);

assert.deepStrictEqual(length, 1);
assert.deepStrictEqual(actual, expect);
}
},
60 * 1000,
);
});

0 comments on commit b3dbc90

Please sign in to comment.