Skip to content

Commit e829f8f

Browse files
committed
Merge pull request #1 from benjamn/run-tests-in-iframes
Run each test in its own <iframe>
2 parents 9425e58 + 603c9ef commit e829f8f

24 files changed

+241
-79
lines changed

Gruntfile.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,9 @@ module.exports = function(grunt) {
5353
grunt.registerTask('build:min', ['jsx:release', 'browserify:min']);
5454
grunt.registerTask('build:test', [
5555
'jsx:debug',
56+
'jsx:jasmine',
5657
'jsx:test',
58+
'browserify:jasmine',
5759
'browserify:test'
5860
]);
5961

grunt/config/browserify.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,23 @@ var transformer = {
7676
after: [simpleBannerify]
7777
};
7878

79+
var jasmine = {
80+
entries: [
81+
"./build/jasmine/all.js"
82+
],
83+
requires: {
84+
"jasmine": "./build/jasmine/all.js"
85+
},
86+
outfile: "./build/jasmine.js",
87+
debug: false
88+
};
89+
7990
var test = {
8091
entries: [
8192
"./build/modules/test/all.js",
82-
"./build/modules/**/__tests__/*-test.js"
93+
],
94+
requires: [
95+
"**/__tests__/*-test.js"
8396
],
8497
outfile: './build/react-test.js',
8598
debug: false,
@@ -88,6 +101,7 @@ var test = {
88101

89102
module.exports = {
90103
basic: basic,
104+
jasmine: jasmine,
91105
test: test,
92106
min: min,
93107
transformer: transformer

grunt/config/jsx/jsx.js

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,40 @@ var rootIDs = [
66

77
var debug = {
88
rootIDs: rootIDs,
9-
configFile: "grunt/config/jsx/debug.json"
9+
configFile: "grunt/config/jsx/debug.json",
10+
sourceDir: "src",
11+
outputDir: "build/modules"
12+
};
13+
14+
var jasmine = {
15+
rootIDs: [
16+
"all"
17+
],
18+
configFile: debug.configFile,
19+
sourceDir: "vendor/jasmine",
20+
outputDir: "build/jasmine"
1021
};
1122

1223
var test = {
1324
rootIDs: rootIDs.concat([
1425
"test/all.js",
1526
"**/__tests__/*.js"
1627
]),
17-
configFile: debug.configFile
28+
configFile: debug.configFile,
29+
sourceDir: "src",
30+
outputDir: "build/modules"
1831
};
1932

2033
var release = {
2134
rootIDs: rootIDs,
22-
configFile: "grunt/config/jsx/release.json"
35+
configFile: "grunt/config/jsx/release.json",
36+
sourceDir: "src",
37+
outputDir: "build/modules"
2338
};
2439

2540
module.exports = {
2641
debug: debug,
42+
jasmine: jasmine,
2743
test: test,
2844
release: release
2945
};

grunt/config/phantom.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,8 @@ exports.run = {
44
port: 8080,
55
harness: "test/phantom-harness.js",
66
// Run `grunt test --debug` to enable in-browser testing.
7-
debug: !!grunt.option("debug")
7+
debug: !!grunt.option("debug"),
8+
tests: [
9+
"**/__tests__/*-test.js"
10+
]
811
};

grunt/tasks/browserify.js

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ module.exports = function() {
1212
// More/better assertions
1313
// grunt.config.requires('outfile');
1414
// grunt.config.requires('entries');
15-
config.requires = config.requires || {};
1615
config.transforms = config.transforms || [];
1716
config.after = config.after || [];
1817
if (typeof config.after === 'function') {
@@ -24,9 +23,20 @@ module.exports = function() {
2423
var bundle = browserify(entries);
2524

2625
// Make sure the things that need to be exposed are.
27-
// TODO: support a blob pattern maybe?
28-
for (var name in config.requires) {
29-
bundle.require(config.requires[name], { expose: name });
26+
var requires = config.requires || {};
27+
if (requires instanceof Array) {
28+
grunt.file.expand({
29+
nonull: true, // Keep IDs that don't expand to anything.
30+
cwd: "src"
31+
}, requires).forEach(function(name) {
32+
bundle.require("./build/modules/" + name, {
33+
expose: name.replace(/\.js$/i, "")
34+
});
35+
});
36+
} else if (typeof requires === "object") {
37+
Object.keys(requires).forEach(function(name) {
38+
bundle.require(requires[name], { expose: name });
39+
});
3040
}
3141

3242
// Extract other options

grunt/tasks/jsx.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ module.exports = function() {
99

1010
var args = [
1111
"bin/jsx",
12-
"src",
13-
"build/modules"
12+
config.sourceDir,
13+
config.outputDir
1414
];
1515

1616
var rootIDs = expand({

grunt/tasks/phantom.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,14 @@ function run(config, done) {
3737
args.push("--debug");
3838
}
3939

40+
args.push("--tests");
41+
var tests = grunt.file.expand({
42+
nonull: true,
43+
cwd: "src"
44+
}, config.tests || []).forEach(function(file) {
45+
args.push(file.replace(/\.js$/i, ""));
46+
});
47+
4048
var child = spawn({
4149
cmd: phantomjs,
4250
args: args

src/test/all.js

Lines changed: 48 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,56 @@
22
// modules in src/test and to specify an ordering on those modules, since
33
// some still have implicit dependencies on others.
44

5-
require("./phantom");
6-
require("./console");
5+
var Ap = Array.prototype;
6+
var slice = Ap.slice;
7+
var Fp = Function.prototype;
8+
9+
if (!Fp.bind) {
10+
// PhantomJS doesn't support Function.prototype.bind natively, so
11+
// polyfill it whenever this module is required.
12+
Fp.bind = function(context) {
13+
var func = this;
14+
var args = slice.call(arguments, 1);
15+
var bound;
16+
17+
if (func.prototype) {
18+
if (args.length > 0) {
19+
bound = function() {
20+
return func.apply(
21+
!(this instanceof func) && context || this,
22+
args.concat(slice.call(arguments))
23+
);
24+
};
25+
} else {
26+
bound = function() {
27+
return func.apply(
28+
!(this instanceof func) && context || this,
29+
arguments
30+
);
31+
};
32+
}
33+
34+
bound.prototype = Object.create(func.prototype);
35+
36+
} else if (args.length > 0) {
37+
bound = function() {
38+
return func.apply(
39+
context || this,
40+
args.concat(slice.call(arguments))
41+
);
42+
};
43+
} else {
44+
bound = function() {
45+
return func.apply(context || this, arguments);
46+
};
47+
}
48+
49+
return bound;
50+
};
51+
}
52+
753
require("ReactTestUtils");
854
require("reactComponentExpect");
9-
require("./diff");
10-
require("./PrintReporter");
11-
require("./HtmlReporter");
12-
require("./ReporterView");
13-
require("./SpecView");
14-
require("./SuiteView");
15-
require("./jasmine-support");
1655
require("mocks");
1756
require("mock-modules");
1857
require("./mock-timers");

src/utils/__tests__/ImmutableObject-test.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,20 +100,23 @@ describe('ImmutableObject', function() {
100100
});
101101

102102
testDev('should prevent shallow field addition when strict', function() {
103+
if (window.callPhantom) return;
103104
expect(function() {
104105
var io = new ImmutableObject({oldField: 'asdf'});
105106
io.newField = 'this will not work';
106107
}).toThrow();
107108
});
108109

109110
testDev('should prevent shallow field mutation when strict', function() {
111+
if (window.callPhantom) return;
110112
expect(function() {
111113
var io = new ImmutableObject({oldField: 'asdf'});
112114
io.oldField = 'this will not work!';
113115
}).toThrow();
114116
});
115117

116118
testDev('should prevent deep field addition when strict', function() {
119+
if (window.callPhantom) return;
117120
expect(function() {
118121
var io =
119122
new ImmutableObject({shallowField: {deepField: {oldField: null}}});
@@ -122,6 +125,7 @@ describe('ImmutableObject', function() {
122125
});
123126

124127
testDev('should prevent deep field mutation when strict', function() {
128+
if (window.callPhantom) return;
125129
expect(function() {
126130
var io =
127131
new ImmutableObject({shallowField: {deepField: {oldField: null}}});

test/frame.html

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<script>
5+
jasmine = parent.jasmine;
6+
jasmine.exposeFrom(window);
7+
8+
console = parent.console;
9+
callPhantom = parent.callPhantom;
10+
</script>
11+
<script src="react-test.js"></script>
12+
</head>
13+
<body>
14+
<script>
15+
require(window.frameElement.getAttribute("test"));
16+
</script>
17+
</body>
18+
</html>

0 commit comments

Comments
 (0)