我已经创建了一个易于测试的原理图,因为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模板。
发布于 2019-02-20 23:41:29
我想出了一个方法来做到这一点。我创建了一个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 import。
{
"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));您可以查看完整的测试here。
https://stackoverflow.com/questions/54660312
复制相似问题