0

I've created a schematic that it easy to test because Angular CLI is based on schematics. To create a tree of files that I can inspect, all I have to do is setup an appTree as follows:

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);
});

Is it possible to create a similar tree of files that I can use when testing my app on a React or Vue project (generated with Create React App and Vue CLI, respectively)?

If not, is there a way to load up a package.json file into the tree? That's the main file I want to load in order to look for dependencies and choose Angular, React, or Vue templates from my Schematic.

Matt Raible
  • 8,187
  • 9
  • 61
  • 120

1 Answers1

0

I figured out a way to do this. I created a 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"
  ]
}

Then I modified my tsconfig.json to support JSON import.

{
  "compilerOptions": {
    "resolveJsonModule": true,
    "esModuleInterop": true  
  }
}

Then I imported this file in my test:

import packageJson from './react-pkg.json';

And then added it to the tree:

const tree = new UnitTestTree(new HostTree);

// Add package.json
tree.create('/package.json', JSON.stringify(packageJson));

You can see the full test here.

Matt Raible
  • 8,187
  • 9
  • 61
  • 120