UNPKG

@yolkai/nx-workspace

Version:

Extensible Dev Tools for Monorepos

91 lines (90 loc) 4.39 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); const testing_1 = require("@angular-devkit/schematics/testing"); const schematics_1 = require("@angular-devkit/schematics"); const prettier = require("prettier"); const path = require("path"); const format_files_1 = require("./format-files"); const appRoot = require("app-root-path"); describe('formatFiles', () => { let tree; let schematicRunner; beforeEach(() => { schematicRunner = new testing_1.SchematicTestRunner('@yolkai/nx-workspace', path.join(__dirname, '../../../collection.json')); spyOn(prettier, 'format').and.callFake(input => 'formatted :: ' + input); tree = schematics_1.Tree.empty(); }); it('should format created files', () => __awaiter(this, void 0, void 0, function* () { spyOn(prettier, 'resolveConfig').and.returnValue(Promise.resolve({ printWidth: 80 })); tree.create('a.ts', 'const a=a'); const result = yield schematicRunner .callRule(format_files_1.formatFiles(), tree) .toPromise(); expect(prettier.format).toHaveBeenCalledWith('const a=a', { printWidth: 80, filepath: appRoot.resolve('a.ts') }); expect(result.read('a.ts').toString()).toEqual('formatted :: const a=a'); })); it('should not format deleted files', () => __awaiter(this, void 0, void 0, function* () { spyOn(prettier, 'resolveConfig').and.returnValue(Promise.resolve({ printWidth: 80 })); tree.create('b.ts', ''); tree.delete('b.ts'); yield schematicRunner.callRule(format_files_1.formatFiles(), tree).toPromise(); expect(prettier.format).not.toHaveBeenCalledWith('const b=b', jasmine.anything()); })); it('should format overwritten files', () => __awaiter(this, void 0, void 0, function* () { spyOn(prettier, 'resolveConfig').and.returnValue(Promise.resolve(null)); tree.create('a.ts', 'const a=a'); tree.overwrite('a.ts', 'const a=b'); const result = yield schematicRunner .callRule(format_files_1.formatFiles(), tree) .toPromise(); expect(prettier.format).toHaveBeenCalledWith('const a=b', { filepath: appRoot.resolve('a.ts') }); expect(result.read('a.ts').toString()).toEqual('formatted :: const a=b'); })); it('should not format renamed files', () => __awaiter(this, void 0, void 0, function* () { spyOn(prettier, 'resolveConfig').and.returnValue(Promise.resolve(null)); tree.create('a.ts', 'const a=a'); tree.rename('a.ts', 'b.ts'); const result = yield schematicRunner .callRule(format_files_1.formatFiles(), tree) .toPromise(); expect(prettier.format).toHaveBeenCalledWith('const a=a', { filepath: appRoot.resolve('b.ts') }); expect(result.read('b.ts').toString()).toEqual('formatted :: const a=a'); })); describe('--skip-format', () => { it('should not format created files', () => __awaiter(this, void 0, void 0, function* () { spyOn(prettier, 'resolveConfig').and.returnValue(Promise.resolve({ printWidth: 80 })); tree.create('a.ts', 'const a=a'); const result = yield schematicRunner .callRule(format_files_1.formatFiles({ skipFormat: true }), tree) .toPromise(); expect(prettier.format).not.toHaveBeenCalledWith('const a=a', { printWidth: 80, filepath: appRoot.resolve('a.ts') }); expect(result.read('a.ts').toString()).toEqual('const a=a'); })); }); });