Skip to content

Commit

Permalink
Merge pull request #3 from gaearon/compiler-test-fixtures
Browse files Browse the repository at this point in the history
Add compiler test fixture infra
  • Loading branch information
trueadm committed Sep 15, 2017
2 parents 351f734 + 1ca8b84 commit 11858a7
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 0 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@
"lint": "node ./scripts/tasks/eslint.js",
"postinstall": "node node_modules/fbjs-scripts/node/check-dev-engines.js package.json",
"test": "jest",
"test:compiler": "jest Compiler-test",
"flow": "node ./scripts/tasks/flow.js",
"prettier": "node ./scripts/prettier/index.js write-changed",
"prettier-all": "node ./scripts/prettier/index.js write",
Expand Down
54 changes: 54 additions & 0 deletions scripts/compiler-fixtures/__tests__/Compiler-test.js
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');
});
});
28 changes: 28 additions & 0 deletions scripts/compiler-fixtures/__tests__/fixtures/simple.js
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;
19 changes: 19 additions & 0 deletions scripts/compiler-fixtures/__tests__/transform.js
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');
};

0 comments on commit 11858a7

Please sign in to comment.