1# compiler unit testing 2 3**The re-built compiler unit testing for ace2.0.** 4 5## Usage 6 7Here are simplified instructions of how to get started. The following commands work both on **Windows** and **Linux** platforms. 8 9### 1. Install 10 11First, enter the root directory of the compiler: 12``` 13$ cd compiler/ 14``` 15And then install the npm dependencies(You must have node&npm installed): 16``` 17$ npm install 18``` 19**Note**: If some errors occur, delete the generated package `node_modules`, config npm proxy and run `npm install` again. 20``` 21npm config set proxy http://username:password@server:port 22npm confit set https-proxy http://username:password@server:port 23``` 24 25### 2. Quick Start 26 27In the root directory of `compiler/`: 28``` 29$ npm run etsTest 30``` 31All test files in the `compiler/test/transform_ut` will be tested. 32 33There are three test scenes, which is the same as the test cases with script command `npm run test`, are: 34 35- `ut`: Testing compiled .js file result from corresponding .ets files in the non-partial-update mode. 36- `utForPartialUpdate`: Testing compiled .js file result from corresponding .ets files in the partial update mode. 37- `utForValidate`: Testing expected catched error/warning logs result when compiling .ets files in the partial update mode. 38 39### 3. Test Framework Overview 40 41The re-built test framework is to mock a rollup object and create a DevEco sample application for testing. 42 43In each test scene, the framework first configures shared parameters, then calls `buildStart()` hook from `etsChecker` rollup plugin. Then for each test case, the framework calls `transform()` hook from `etsTransform` rollup plugin. Finally, the framework compares and tests the results (i.e. compiled file content or error/warning logs) with the expected results. 44 45The file structure of the test framework shows as follows: 46 47```yaml 48ace_ets2bundle/compiler/ 49└──test 50 ├── transform_ut 51 │ ├── application/entry # DevEco sample application 52 │ │ ├── build/default/cache/default/default@CompileArkTS/esmodule 53 │ │ │ └── debug/entry/src/main/ets/pages 54 │ │ │ ├── ut # ut compiled .js files 55 │ │ │ └── utForPartialUpdate # utForPartialUpdate compiled .js files 56 │ │ └── src 57 │ │ └── main/ets 58 │ │ │ ├── entryability 59 │ │ │ ├── entrybackupability 60 │ │ │ ├── pages 61 │ │ │ │ ├── ut # ut .ets source files 62 │ │ │ │ ├── utForPartialUpdate # utForPartialUpdate .ets source files 63 │ │ │ │ └── utForValidate # utForValidate .ets source files 64 │ │ │ └── test # external files used for import 65 │ │ └── mock 66 │ ├── helpers # test framework utils 67 │ │ ├── cache.ts # mock cache 68 │ │ ├── common.ts # common params 69 │ │ ├── logger.ts # mock logger 70 │ │ ├── mockRollupContext.ts # mock rollup 71 │ │ ├── module.ts # mock moduleInfo 72 │ │ ├── parser.ts # text parsing utils 73 │ │ ├── pathConfig.ts # file path configuration 74 │ │ ├── projectConfig.ts # projectConfig configuration 75 │ │ ├── share.ts # mock rollup share 76 │ │ └── utils.ts # other utils 77 │ ├── rollup_share.test.ts # test mock rollup 78 │ ├── ut_partial_update.test.ts # test utForPartialUpdate cases 79 │ ├── ut_validate.test.ts # test utForValidate cases 80 │ └── ut.test.ts # test ut cases 81 └── transform_ut_error.json # expected error logs in utForValidate 82``` 83 84### 3. Add New Test Cases 85 86You can add new test cases in different test scenes. 87 881. **Add .ets file as the source file**: go to directory `compiler/test/transform_ut/application/entry/src/main/ets/pages`, find the test scenes that you want to add (i.e. `ut`, `utForPartialUpdate`, or `utForValidate`), and then add a new test directory and put the .ets file inside. 892. **Add expected result**: According to the test scene that you want to add, 90 - If test scene is `ut`, then go to directory `compiler/test/transform_ut/application/entry/build/default/cache/default/default@CompileArkTS/esmodule/debug/entry/src/main/ets/pages/ut`, and then add a new test directory (same as the name used before) and put the expected compiled .js file inside. 91 - If test scene is `utForPartialUpdate`, then go to directory `compiler/test/transform_ut/application/entry/build/default/cache/default/default@CompileArkTS/esmodule/debug/entry/src/main/ets/pages/utForPartialUpdate`, and then add a new test directory (same as the name used before) and put the expected compiled .js file inside. 92 - If test scene is `utForValidate`, then open JSON file at `compiler/test/transform_ut_error.json`, and then add a new entry (same as the name used before) and put the expected error/wanrning logs inside. 933. **Add new test case**: Open file path configuration file at `compiler/test/transform_ut/helpers/pathConfig.ts`. According to the test scene that you want to add, 94 - If test scene is `ut`, then add partial path name for the new test file you just created in to `UT_PAGES`. The partial path name is in the following template: `<new test directory name>/<new test file name>`. 95 - If test scene is `utForPartialUpdate`, then add partial path name for the new test file you just created in to `UT_PARTIAL_UPFATE_PAGES`. The partial path name is in the following template: `<new test directory name>/<new test file name>`. 96 - If test scene is `utForValidate`, then add partial path name for the new test file you just created in to `UT_VALIDATE_PAGES`. The partial path name is in the following template: `<new test directory name>/<new test file name>`. 974. **Run test**: run script command `npm run etsTest` to run all test cases in the `compiler/test/transform_ut`.