Skip to content

Commit

Permalink
chore: add a test for the react production bundle
Browse files Browse the repository at this point in the history
It's failing, see discussion in bazel-contrib#933
  • Loading branch information
alexeagle committed Sep 10, 2019
1 parent 2ccb9f1 commit e64cb65
Show file tree
Hide file tree
Showing 19 changed files with 903 additions and 29 deletions.
8 changes: 8 additions & 0 deletions examples/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ example_integration_test(
},
)

example_integration_test(
name = "examples_reactjs",
npm_packages = {
"//packages/jasmine:npm_package": "@bazel/jasmine",
"//packages/typescript:npm_package": "@bazel/typescript",
},
)

example_integration_test(
name = "examples_parcel",
npm_packages = {
Expand Down
32 changes: 20 additions & 12 deletions examples/reactjs/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
load("@build_bazel_rules_nodejs//:defs.bzl", "http_server", "rollup_bundle")
load("@build_bazel_rules_nodejs//internal/web_package:web_package.bzl", "web_package")
load("@npm_bazel_jasmine//:index.bzl", "jasmine_node_test")
load("@npm_bazel_typescript//:index.bzl", "ts_devserver", "ts_library")

package(default_visibility = ["//visibility:public"])

ts_library(
name = "app",
srcs = [
"app.ts",
"greeting.tsx",
"react-fix.ts",
],
srcs = glob([
"*.ts",
"*.tsx",
]),
deps = [
"@npm//@types/react",
"@npm//@types/react-dom",
"@npm//react",
"@npm//react-dom",
],
)

Expand All @@ -37,8 +35,12 @@ ts_devserver(
rollup_bundle(
name = "bundle",
enable_code_splitting = False,
entry_point = ":app.ts",
deps = [":app"],
entry_point = ":app.prod.ts",
deps = [
":app",
"@npm//react",
"@npm//react-dom",
],
)

web_package(
Expand All @@ -59,9 +61,15 @@ http_server(
)

# Just a dummy test so that we have a test target for //... on certain bazelci platforms with bazel_integration_test
sh_test(
name = "dummy_test",
srcs = ["dummy_test.sh"],
jasmine_node_test(
name = "prodmode_test",
srcs = ["prodmode.spec.js"],
data = [
"bundle.es2015.js",
"index.html",
"@npm//domino",
],
templated_args = ["--node_options=--experimental-modules"],
)

# For testing from the root workspace of this repository with bazel_integration_test.
Expand Down
5 changes: 5 additions & 0 deletions examples/reactjs/app.prod.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import './environment.prod';
import * as ReactDOM from 'react-dom';
import greeting from './greeting';

ReactDOM.render(greeting, document.getElementById('root'));
5 changes: 2 additions & 3 deletions examples/reactjs/app.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import './react-fix';
import React from 'react';
import ReactDOM from 'react-dom';
import './environment.dev';
import * as ReactDOM from 'react-dom';
import greeting from './greeting';

ReactDOM.render(greeting, document.getElementById('root'));
1 change: 1 addition & 0 deletions examples/reactjs/env.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(window as any).process = {env: {NODE_ENV: 'production'}}
10 changes: 10 additions & 0 deletions examples/reactjs/environment.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// React uses a "isomorphic" package that relies on
// `process.env`, even in the browser.
// Make this type available in TypeScript code.
declare global {
interface Window {
process: {}
}
}
// make this file a module, not a script
export {};
5 changes: 5 additions & 0 deletions examples/reactjs/environment.dev.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Configure React for development mode
// https://github.com/bazelbuild/rules_nodejs/issues/555
window.process = {
env: {NODE_ENV: 'development'}
}
5 changes: 5 additions & 0 deletions examples/reactjs/environment.prod.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Configure React for production
// https://github.com/bazelbuild/rules_nodejs/issues/555
window.process = {
env: {NODE_ENV: 'production'}
}
3 changes: 1 addition & 2 deletions examples/reactjs/greeting.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import React from 'react';
import ReactDOM from 'react-dom';
import * as React from 'react';

const greeting = <h1>Hello, world</h1>;
export default greeting;
7 changes: 4 additions & 3 deletions examples/reactjs/package.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
{
"devDependencies": {
"@bazel/hide-bazel-files": "latest",
"@bazel/jasmine": "latest",
"@bazel/typescript": "latest",
"@types/react": "^16.9.2",
"@types/react-dom": "^16.9.0",
"domino": "^2.1.3",
"typescript": "2.7.x"
},
"scripts": {
"test": "bazel test ..."
},
"dependencies": {
"@types/react": "^16.9.2",
"@types/react-dom": "^16.9.0",
"react": "^16.9.0",
"react-dom": "^16.9.0"
}
Expand Down
18 changes: 18 additions & 0 deletions examples/reactjs/prodmode.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const domino = require('domino');
const path = require('path');
const fs = require('fs');

describe('react webapp', () => {
it('works', () => {
const html = fs.readFileSync(path.join(__dirname, 'index.html'));
// Domino gives us enough of the DOM API that we can run our JavaScript in node rather than the
// browser. That makes this test a lot faster
global.window = domino.createWindow(html, '/');
global.document = global.window.document;
global.navigator = global.window.navigator;
// Make all Domino types available as types in the global env.
Object.assign(global, domino.impl);

import(path.join(__dirname, 'bundle.es2015')).then(() => {expect(global.document.body.textContent).toEqual('Hello from React!')});
});
});
2 changes: 0 additions & 2 deletions examples/reactjs/react-fix.ts

This file was deleted.

1 change: 1 addition & 0 deletions examples/reactjs/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"compilerOptions": {
"jsx": "react",
"strict": true,
"lib": ["es2015.promise", "dom", "es5"],
// to fix https://github.com/bazelbuild/rules_nodejs/issues/933
Expand Down
Loading

0 comments on commit e64cb65

Please sign in to comment.