-
-
Notifications
You must be signed in to change notification settings - Fork 603
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'next' into fix/help-group
- Loading branch information
Showing
20 changed files
with
294 additions
and
193 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
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 @@ | ||
{} |
24 changes: 24 additions & 0 deletions
24
packages/utils/__tests__/find-project-root/project/find-project-root.test.ts
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,24 @@ | ||
// eslint-disable-next-line node/no-unpublished-import | ||
import { findProjectRoot } from '../../../src/path-utils'; | ||
import { join } from 'path'; | ||
|
||
beforeAll(() => { | ||
// This sets the project root as the root dir, needed since some tests change process cwd. | ||
process.chdir(join(__dirname, '../../../../../')); | ||
}); | ||
|
||
describe('findProjectRoot function', () => { | ||
it('works correctly', () => { | ||
/* when no directory is passed, it takes the current process working directory as starting point | ||
which contains package.json thus it should be the project root */ | ||
const projectRoot = findProjectRoot(); | ||
expect(projectRoot).toEqual(process.cwd()); | ||
}); | ||
|
||
it('works correctly with a non-default dir', () => { | ||
/* when passing a custom directory, it's used as the starting point and so it should yield the path | ||
nearest package.json from the given directory */ | ||
const projectRoot = findProjectRoot(__dirname); | ||
expect(projectRoot).toEqual(join(__dirname, '..')); | ||
}); | ||
}); |
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,42 @@ | ||
'use strict'; | ||
|
||
import fs from 'fs'; | ||
import path from 'path'; | ||
//eslint-disable-next-line node/no-extraneous-import | ||
import rimraf from 'rimraf'; | ||
import { runPrettier } from '../src/run-prettier'; | ||
|
||
const outputPath = path.join(__dirname, 'test-assets'); | ||
const outputFile = path.join(outputPath, 'test.js'); | ||
const stdoutSpy = jest.spyOn(process.stdout, 'write'); | ||
|
||
describe('runPrettier', () => { | ||
beforeEach(() => { | ||
rimraf.sync(outputPath); | ||
fs.mkdirSync(outputPath); | ||
stdoutSpy.mockClear(); | ||
}); | ||
|
||
afterAll(() => { | ||
rimraf.sync(outputPath); | ||
}); | ||
|
||
it('should run prettier on JS string and write file', () => { | ||
runPrettier(outputFile, 'console.log("1");console.log("2");'); | ||
expect(fs.existsSync(outputFile)).toBeTruthy(); | ||
const data = fs.readFileSync(outputFile, 'utf8'); | ||
expect(data).toContain("console.log('1');\n"); | ||
|
||
expect(stdoutSpy.mock.calls.length).toEqual(0); | ||
}); | ||
|
||
it('prettier should fail on invalid JS, with file still written', () => { | ||
runPrettier(outputFile, '"'); | ||
expect(fs.existsSync(outputFile)).toBeTruthy(); | ||
const data = fs.readFileSync(outputFile, 'utf8'); | ||
expect(data).toContain('"'); | ||
|
||
expect(stdoutSpy.mock.calls.length).toEqual(1); | ||
expect(stdoutSpy.mock.calls[0][0]).toContain('WARNING: Could not apply prettier'); | ||
}); | ||
}); |
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
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
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
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,7 @@ | ||
/* eslint-disable */ | ||
|
||
module.exports = { | ||
output: { | ||
badOption: true, | ||
}, | ||
}; |
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,86 @@ | ||
'use strict'; | ||
|
||
const fs = require('fs'); | ||
const path = require('path'); | ||
const rimraf = require('rimraf'); | ||
const { run, runAndGetWatchProc, runPromptWithAnswers } = require('../../utils/test-utils'); | ||
|
||
const ENTER = '\x0D'; | ||
const outputDir = 'test-assets'; | ||
const outputPath = path.join(__dirname, outputDir); | ||
const outputFile = `${outputDir}/updated-webpack.config.js`; | ||
const outputFilePath = path.join(__dirname, outputFile); | ||
|
||
describe('migrate command', () => { | ||
beforeEach(() => { | ||
rimraf.sync(outputPath); | ||
fs.mkdirSync(outputPath); | ||
}); | ||
|
||
afterAll(() => { | ||
rimraf.sync(outputPath); | ||
}); | ||
|
||
it('should warn if the source config file is not specified', () => { | ||
const { stderr } = run(__dirname, ['migrate'], false); | ||
expect(stderr).toContain('Please specify a path to your webpack config'); | ||
}); | ||
|
||
it('should prompt accordingly if an output path is not specified', () => { | ||
const { stdout } = run(__dirname, ['migrate', 'webpack.config.js'], false); | ||
expect(stdout).toContain('? Migration output path not specified'); | ||
}); | ||
|
||
it('should throw an error if the user refused to overwrite the source file and no output path is provided', async () => { | ||
const { stderr } = await runAndGetWatchProc(__dirname, ['migrate', 'webpack.config.js'], false, 'n'); | ||
expect(stderr).toBe('✖ ︎Migration aborted due to no output path'); | ||
}); | ||
|
||
it('should prompt for config validation when an output path is provided', async () => { | ||
const { stdout } = await runAndGetWatchProc(__dirname, ['migrate', 'webpack.config.js', outputFile], false, 'y'); | ||
// should show the diff of the config file | ||
expect(stdout).toContain('rules: ['); | ||
expect(stdout).toContain('? Do you want to validate your configuration?'); | ||
}); | ||
|
||
it('should generate an updated config file when an output path is provided', async () => { | ||
const { stdout, stderr } = await runPromptWithAnswers( | ||
__dirname, | ||
['migrate', 'webpack.config.js', outputFile], | ||
[ENTER, ENTER], | ||
true, | ||
); | ||
expect(stdout).toContain('? Do you want to validate your configuration?'); | ||
// should show the diff of the config file | ||
expect(stdout).toContain('rules: ['); | ||
expect(stderr).toBeFalsy(); | ||
|
||
expect(fs.existsSync(outputFilePath)).toBeTruthy(); | ||
// the output file should be a valid config file | ||
const config = require(outputFilePath); | ||
expect(config.module.rules).toEqual([ | ||
{ | ||
test: /\.js$/, | ||
exclude: /node_modules/, | ||
|
||
use: [ | ||
{ | ||
loader: 'babel-loader', | ||
|
||
options: { | ||
presets: ['@babel/preset-env'], | ||
}, | ||
}, | ||
], | ||
}, | ||
]); | ||
}); | ||
|
||
it('should generate an updated config file and warn of an invalid webpack config', async () => { | ||
const { stdout, stderr } = await runPromptWithAnswers(__dirname, ['migrate', 'bad-webpack.config.js', outputFile], [ENTER, ENTER]); | ||
expect(stdout).toContain('? Do you want to validate your configuration?'); | ||
expect(stderr).toContain("configuration.output has an unknown property 'badOption'"); | ||
|
||
expect(fs.existsSync(outputFilePath)).toBeTruthy(); | ||
}); | ||
}); |
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,32 @@ | ||
/* eslint-disable */ | ||
const path = require('path'); | ||
|
||
module.exports = { | ||
entry: { | ||
index: './src/index.js', | ||
vendor: './src/vendor.js', | ||
}, | ||
|
||
output: { | ||
filename: '[name].[chunkhash].js', | ||
chunkFilename: '[name].[chunkhash].js', | ||
path: path.resolve(__dirname, 'dist'), | ||
}, | ||
|
||
optimization: { | ||
minimize: true | ||
}, | ||
|
||
module: { | ||
loaders: [ | ||
{ | ||
test: /\.js$/, | ||
exclude: /node_modules/, | ||
loader: 'babel', | ||
query: { | ||
presets: ['@babel/preset-env'], | ||
}, | ||
}, | ||
], | ||
}, | ||
}; |
Oops, something went wrong.