我创建了一个易于测试的原理图,因为 Angular CLI 基于原理图。要创建我可以检查的文件树,我所要做的就是设置一个appTree
,如下所示:
const schematicRunner = new SchematicTestRunner(
'schematics',
path.join(__dirname, './../collection.json'),
);
let appTree: UnitTestTree;
// tslint:disable-next-line:no-any
const workspaceOptions: any = {
name: 'workspace',
newProjectRoot: 'projects',
version: '0.5.0',
};
// tslint:disable-next-line:no-any
const appOptions: any = {
name: 'authtest',
inlineStyle: false,
inlineTemplate: false,
routing: false,
style: 'css',
skipTests: false,
};
beforeEach(() => {
appTree = schematicRunner.runExternalSchematic('@schematics/angular', 'workspace', workspaceOptions);
appTree = schematicRunner.runExternalSchematic('@schematics/angular', 'application', appOptions, appTree);
});
是否可以创建一个类似的文件树,在 React 或 Vue 项目(分别使用 Create React App 和 Vue CLI 生成(上测试我的应用程序时可以使用?
如果没有,有没有办法将package.json
文件加载到树中?这是我想要加载的主文件,以便查找依赖项并从我的原理图中选择 Angular、React 或 Vue 模板。
我想出了一种方法来做到这一点。我创建了一个react-pkg.json
:
{
"name": "react-app",
"version": "0.1.0",
"private": true,
"dependencies": {
"react": "^16.8.2",
"react-dom": "^16.8.2",
"react-router-dom": "~4.3.1",
"react-scripts": "2.1.5"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": "react-app"
},
"browserslist": [
">0.2%",
"not dead",
"not ie <= 11",
"not op_mini all"
]
}
然后我修改了tsconfig.json
以支持 JSON 导入。
{
"compilerOptions": {
"resolveJsonModule": true,
"esModuleInterop": true
}
}
然后我在测试中导入了这个文件:
import packageJson from './react-pkg.json';
然后将其添加到树中:
const tree = new UnitTestTree(new HostTree);
// Add package.json
tree.create('/package.json', JSON.stringify(packageJson));
您可以在此处查看完整的测试。