是否可以从Create React App或Vue CLI创建内存树?

时间:2019-02-12 23:34:34

标签: angular reactjs vue.js angular-cli angular-schematics

由于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模板。

1 个答案:

答案 0 :(得分:0)

我想出了一种方法。我创建了一个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