|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const acorn = require('acorn'); |
| 4 | +const assert = require('yeoman-assert'); |
| 5 | +const fs = require('fs-extra'); |
| 6 | +const helpers = require('yeoman-test'); |
| 7 | +const path = require('path'); |
| 8 | +const walk = require('acorn/dist/walk'); |
| 9 | + |
| 10 | + |
| 11 | +/** |
| 12 | + * Returns absolute path to (sub-)generator with {@code name}. |
| 13 | + * @param {string} name |
| 14 | + */ |
| 15 | +const genpath = (name) => |
| 16 | + path.join(__dirname, '../../../generators', name); |
| 17 | + |
| 18 | +/** |
| 19 | + * A mocked generator config object. |
| 20 | + * @type {{appName: string, style: string, cssmodules: boolean, postcss: boolean, generatedWithVersion: number}} |
| 21 | + */ |
| 22 | +const cfg = { |
| 23 | + appName: 'testCfg', |
| 24 | + style: 'css', |
| 25 | + cssmodules: false, |
| 26 | + postcss: false, |
| 27 | + generatedWithVersion: 4 |
| 28 | +}; |
| 29 | + |
| 30 | + |
| 31 | +describe('react-webpack:setup-env', function () { |
| 32 | + |
| 33 | + describe('react-webpack:setup-env foobar', function () { |
| 34 | + before(function () { |
| 35 | + return helpers |
| 36 | + .run(genpath('setup-env')) |
| 37 | + .withArguments(['foobar']) |
| 38 | + .withLocalConfig(cfg) |
| 39 | + .inTmpDir(function (dir) { |
| 40 | + fs.copySync( |
| 41 | + path.join(__dirname, 'assets/moduleIndex.js'), |
| 42 | + path.join(dir, 'conf/webpack/index.js') |
| 43 | + ); |
| 44 | + }) |
| 45 | + .toPromise(); |
| 46 | + }); |
| 47 | + |
| 48 | + it('creates env files', function () { |
| 49 | + assert.file(['conf/webpack/Foobar.js']); |
| 50 | + assert.file(['src/config/foobar.js']); |
| 51 | + }); |
| 52 | + |
| 53 | + it('requires the new env in conf/webpack/index.js', function () { |
| 54 | + assert.fileContent( |
| 55 | + 'conf/webpack/index.js', |
| 56 | + /const foobar = require\('\.\/Foobar'\)/ |
| 57 | + ); |
| 58 | + }); |
| 59 | + |
| 60 | + it('exports the new env from conf/webpack/index.js', function () { |
| 61 | + const fileStr = fs.readFileSync('conf/webpack/index.js').toString(); |
| 62 | + const ast = acorn.parse(fileStr); |
| 63 | + |
| 64 | + let found = false; |
| 65 | + walk.simple(ast, { |
| 66 | + 'Property': (node) => { |
| 67 | + if (node.key.name === 'foobar' && node.value.name === 'foobar') { |
| 68 | + found = true; |
| 69 | + } |
| 70 | + } |
| 71 | + }); |
| 72 | + |
| 73 | + assert(found, 'Did not find a key and value of `foobar` on the module.exports AST node'); |
| 74 | + }); |
| 75 | + }); |
| 76 | + |
| 77 | +}); |
0 commit comments