Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more tests #116

Merged
merged 9 commits into from
Apr 18, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions lib/creator/transformations/node/__snapshots__/node.test.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`node transforms correctly using "node-0" data 1`] = `
"module.exports = {
entry: 'index.js',
output: {
filename: 'bundle.js'
},

node: {
console: false,
global: true,
process: true,
Buffer: true,
__filename: mock,
__dirname: mock,
setImmediate: true
}
}
"
`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
entry: 'index.js',
output: {
filename: 'bundle.js'
}
}
11 changes: 11 additions & 0 deletions lib/creator/transformations/node/node.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const defineTest = require('../../../transformations/defineTest');

defineTest(__dirname, 'node', 'node-0', {node : {
console: false,
global: true,
process: true,
Buffer: true,
__filename: 'mock',
__dirname: 'mock',
setImmediate: true
}});
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`plugins transforms correctly using "plugins-0" data 1`] = `
"module.exports = {
entry: 'index.js',
output: {
filename: 'bundle.js'
},

plugins: [
new webpack.optimize.CommonsChunkPlugin({name:'vendor',filename:'vendor-[hash].min.js'})
]
}
"
`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
entry: 'index.js',
output: {
filename: 'bundle.js'
}
}
5 changes: 5 additions & 0 deletions lib/creator/transformations/plugins/plugins.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const defineTest = require('../../../transformations/defineTest');

defineTest(__dirname, 'plugins', 'plugins-0', {plugins : [
'new webpack.optimize.CommonsChunkPlugin({name:' + '\'' + 'vendor' + '\'' + ',filename:' + '\'' + 'vendor' + '-[hash].min.js\'})'
]});
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`target transforms correctly using "target-0" data 1`] = `
"module.exports = {
entry: 'index.js',
output: {
filename: 'bundle.js'
},

target: async-node
}
"
`;

exports[`target transforms correctly using "target-1" data 1`] = `
"module.exports = {
entry: 'index.js',
output: {
filename: 'bundle.js'
},

target: 'node',
target: async-node
}
"
`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
entry: 'index.js',
output: {
filename: 'bundle.js'
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = {
entry: 'index.js',
output: {
filename: 'bundle.js'
},
target: 'node'
}
4 changes: 4 additions & 0 deletions lib/creator/transformations/target/target.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const defineTest = require('../../../transformations/defineTest');

defineTest(__dirname, 'target', 'target-0', {target : 'async-node'});
defineTest(__dirname, 'target', 'target-1', {target : 'async-node'});
49 changes: 49 additions & 0 deletions lib/creator/transformations/watch/__snapshots__/watch.test.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`watch transforms correctly using "watch-0" data 1`] = `
"module.exports = {
entry: 'index.js',
output: {
filename: 'bundle.js'
},

watch: true
}
"
`;

exports[`watch transforms correctly using "watch-0" data 2`] = `
"module.exports = {
entry: 'index.js',
output: {
filename: 'bundle.js'
},

watch: false
}
"
`;

exports[`watch transforms correctly using "watch-1" data 1`] = `
"module.exports = {
entry: 'index.js',
output: {
filename: 'bundle.js'
},
watch: true,
watchOptions: {}
}
"
`;

exports[`watch transforms correctly using "watch-1" data 2`] = `
"module.exports = {
entry: 'index.js',
output: {
filename: 'bundle.js'
},
watch: false,
watchOptions: {}
}
"
`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`watchOptions transforms correctly using "watch-0" data 1`] = `
"module.exports = {
entry: 'index.js',
output: {
filename: 'bundle.js'
},

watchOptions: {
aggregateTimeout: 300,
poll: 1000,
ignored: /node_modules/
}
}
"
`;

exports[`watchOptions transforms correctly using "watch-1" data 1`] = `
"module.exports = {
entry: 'index.js',
output: {
filename: 'bundle.js'
},

watch: false,
watchOptions: {
aggregateTimeout: 300,
poll: 1000,
ignored: /node_modules/
},

watchOptions: {
aggregateTimeout: 300,
poll: 1000,
ignored: /node_modules/
}
}
"
`;

exports[`watchOptions transforms correctly using "watch-2" data 1`] = `
"module.exports = {
entry: 'index.js',
output: {
filename: 'bundle.js'
},

watch: false,
watchOptions: {
aggregateTimeout: 320,
aggregateTimeout: 300,
poll: 1000,
ignored: /node_modules/
},

watchOptions: {
aggregateTimeout: 300,
poll: 1000,
ignored: /node_modules/
}
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Failing test case

}
"
`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
entry: 'index.js',
output: {
filename: 'bundle.js'
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module.exports = {
entry: 'index.js',
output: {
filename: 'bundle.js'
},
watch: false,
watchOptions: {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module.exports = {
entry: 'index.js',
output: {
filename: 'bundle.js'
},
watch: false,
watchOptions: {
aggregateTimeout: 320
}
}
8 changes: 5 additions & 3 deletions lib/creator/transformations/watch/watch.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
const isAssignment = require('../../utils/is-assignment');
const transformUtils = require('../../../transformations/utils');

module.exports = function(j, ast, yeomanConfig) {
const webpackProperties = yeomanConfig.webpackOptions;
function createWatchProperty(p) {
return p.value.properties.push(
j.property('init', j.identifier('watch'), j.literal(webpackProperties['watch']))
);
const propValue = transformUtils.createIdentifierOrLiteral(j, webpackProperties['watch']);
transformUtils.checkIfExistsAndAddValue(j, p, 'watch', propValue);
}
if(typeof(webpackProperties['watch']) === 'boolean') {
return ast.find(j.ObjectExpression).filter(p => isAssignment(p, createWatchProperty));
} else {
return ast;
}
};


6 changes: 6 additions & 0 deletions lib/creator/transformations/watch/watch.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const defineTest = require('../../../transformations/defineTest');

defineTest(__dirname, 'watch', 'watch-0', {watch : true});
defineTest(__dirname, 'watch', 'watch-0', {watch : false});
defineTest(__dirname, 'watch', 'watch-1', {watch : true});
defineTest(__dirname, 'watch', 'watch-1', {watch : false});
19 changes: 19 additions & 0 deletions lib/creator/transformations/watch/watchOptions.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const defineTest = require('../../../transformations/defineTest');

defineTest(__dirname, 'watchOptions', 'watch-0', {watchOptions : {
aggregateTimeout: 300,
poll: 1000,
ignored: '/node_modules/'
}});

defineTest(__dirname, 'watchOptions', 'watch-1', {watchOptions : {
aggregateTimeout: 300,
poll: 1000,
ignored: '/node_modules/'
}});

defineTest(__dirname, 'watchOptions', 'watch-2', {watchOptions : {
aggregateTimeout: 300,
poll: 1000,
ignored: '/node_modules/'
}});
17 changes: 17 additions & 0 deletions lib/transformations/__snapshots__/utils.test.js.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`utils checkIfExistsAndAddValue should create new prop if none exist 1`] = `
"
module.exports = {
entry: 'index.js',
externals: \\"React\\"
}
"
`;

exports[`utils checkIfExistsAndAddValue should override prop if it exists 1`] = `
"
module.exports = {
entry: \\"app.js\\"
}
"
`;

exports[`utils createIdentifierOrLiteral should create basic literal 1`] = `"'stringLiteral'"`;

exports[`utils createIdentifierOrLiteral should create boolean 1`] = `"true"`;
Expand Down
26 changes: 25 additions & 1 deletion lib/transformations/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,29 @@ function getRequire(j, constName, packagePath) {
]);
}

/*
* @function checkIfExistsAndAddValue
*
* If a prop exists, it overrides it, else it creates a new one
* @param j — jscodeshift API
* @param { Node } node - objectexpression to check
* @param { string } key - Key of the property
* @param { string } value - computed value of the property
* @returns - nothing
*/

function checkIfExistsAndAddValue(j, node, key, value) {
const existingProp = node.value.properties.filter(prop => prop.key.name === key);
let prop;
if (existingProp.length > 0){
prop = existingProp[0];
prop.value = value;
} else {
prop = j.property('init', j.identifier(key), value);
node.value.properties.push(prop);
}
}

module.exports = {
safeTraverse,
createProperty,
Expand All @@ -282,5 +305,6 @@ module.exports = {
createLiteral,
createIdentifierOrLiteral,
findObjWithOneOfKeys,
getRequire
getRequire,
checkIfExistsAndAddValue
};
21 changes: 21 additions & 0 deletions lib/transformations/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,4 +176,25 @@ var a = { plugs: [] }
expect(j(require).toSource()).toMatchSnapshot();
});
});

describe('checkIfExistsAndAddValue', () => {
it('should create new prop if none exist', () => {
const ast = j(`
module.exports = {
entry: 'index.js'
}
`);
ast.find(j.ObjectExpression).forEach(node => utils.checkIfExistsAndAddValue(j, node, 'externals', j.literal('React')));
expect(ast.toSource()).toMatchSnapshot();
});
it('should override prop if it exists', () => {
const ast = j(`
module.exports = {
entry: 'index.js'
}
`);
ast.find(j.ObjectExpression).forEach(node => utils.checkIfExistsAndAddValue(j, node, 'entry', j.literal('app.js')));
expect(ast.toSource()).toMatchSnapshot();
});
});
});