forked from facebook/react
-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from gaearon/compiler-test-fixtures
Add compiler test fixture infra
- Loading branch information
Showing
4 changed files
with
102 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/** | ||
* Copyright (c) 2013-present, Facebook, Inc. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
* | ||
* @flow | ||
*/ | ||
'use strict'; | ||
|
||
const babel = require('babel-core'); | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
const React = require('ReactEntry'); | ||
const ReactTestRenderer = require('ReactTestRendererFiberEntry'); | ||
const transform = require('./transform'); | ||
|
||
function runSource(code) { | ||
const codeAfterBabel = babel.transform(code, { | ||
presets: ['babel-preset-react'], | ||
plugins: ['transform-object-rest-spread'], | ||
}).code; | ||
const fn = new Function('React', 'module', codeAfterBabel); | ||
let module = {exports: null}; | ||
fn(React, module); | ||
return module.exports; | ||
} | ||
|
||
function getOutput(Root) { | ||
const renderer = ReactTestRenderer.create(<Root />); | ||
// TODO: test updates, unmounting too | ||
return renderer.toJSON(); | ||
} | ||
|
||
async function runFixture(name) { | ||
const src = fs.readFileSync(path.join(__dirname, name)).toString(); | ||
const transformed = await transform(src); | ||
|
||
const After = runSource(transformed); | ||
expect(typeof After).toBe('function'); | ||
|
||
const Before = runSource(src); | ||
expect(typeof Before).toBe('function'); | ||
|
||
expect(getOutput(After)).toEqual(getOutput(Before)); | ||
} | ||
|
||
describe('Compiler', () => { | ||
it('does something', async () => { | ||
await runFixture('fixtures/simple.js'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/** | ||
* Copyright (c) 2013-present, Facebook, Inc. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
*/ | ||
'use strict'; | ||
|
||
function A() { | ||
return <div>Hello</div>; | ||
} | ||
|
||
function B() { | ||
return <div>World</div>; | ||
} | ||
|
||
function C() { | ||
return ( | ||
<div> | ||
<A /> | ||
<B /> | ||
</div> | ||
); | ||
} | ||
|
||
module.exports = C; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/** | ||
* Copyright (c) 2013-present, Facebook, Inc. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
* | ||
* @flow | ||
*/ | ||
'use strict'; | ||
|
||
module.exports = async function(src: string) { | ||
// Put the real compilation call here | ||
await Promise.resolve(); | ||
return src; | ||
// Uncomment to see a failure: | ||
// return src.replace('World', 'Dominic'); | ||
}; |