Skip to content
This repository has been archived by the owner on Jan 5, 2022. It is now read-only.

Add tests for utils #26

Merged
merged 11 commits into from
Dec 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/utils/git-ignore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,6 @@ export function modifyGitIgnore(projectDir: string) {
} else {
cli.warn('No .gitignore file found!');
cli.log('If your project is using a different version control system, please make sure the following paths are not tracked:');
pathsToIgnore.forEach(path => cli.log(' ' + path));
}
}
2 changes: 1 addition & 1 deletion src/utils/usage-analytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import cli from 'cli-ux';
import * as fs from 'fs';
import * as path from 'path';

export async function usageAnalytics(projectDir: string, agreeToAnalytics: boolean, salt?: string) {
export async function usageAnalytics(projectDir: string, agreeToAnalytics: boolean | undefined, salt?: string) {
const analyticsFilePath = path.resolve(projectDir, 'sap-cloud-sdk-analytics.json');
if (agreeToAnalytics === false) {
return fs.writeFileSync(analyticsFilePath, JSON.stringify({ enabled: false }));
Expand Down
4 changes: 2 additions & 2 deletions test/init.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ function getCleanProjectDir(name: string) {
}

describe('Init', () => {
const expressAppDir = 'test/express/';
const nestAppDir = 'test/nest/';
const expressAppDir = path.resolve('test', 'express');
const nestAppDir = path.resolve('test', 'nest');

beforeAll(() => {
if (!fs.existsSync(pathPrefix)) {
Expand Down
43 changes: 43 additions & 0 deletions test/utils/__snapshots__/jest-config.spec.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Jest Config Utils returns jest config object for tests 1`] = `
Object {
"collectCoverage": true,
"coverageDirectory": "../s4hana_pipeline/reports/coverage-reports/backend-unit",
"coverageReporters": Array [
"text",
"cobertura",
],
"reporters": Array [
"default",
Array [
"jest-junit",
Object {
"outputDirectory": "./s4hana_pipeline/reports/backend-unit",
"suiteName": "backend unit tests",
},
],
],
}
`;

exports[`Jest Config Utils returns jest config object for tests 2`] = `
Object {
"collectCoverage": true,
"coverageDirectory": "../s4hana_pipeline/reports/coverage-reports/backend-integration",
"coverageReporters": Array [
"text",
"cobertura",
],
"reporters": Array [
"default",
Array [
"jest-junit",
Object {
"outputDirectory": "./s4hana_pipeline/reports/backend-integration",
"suiteName": "backend unit tests",
},
],
],
}
`;
172 changes: 172 additions & 0 deletions test/utils/__snapshots__/package-json.spec.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Package Json Utils add scripts, dependencies and test config for existing project 1`] = `
Object {
"author": "",
"dependencies": Object {
"@nestjs/common": "^6.0.0",
"@nestjs/core": "^6.0.0",
"@nestjs/platform-express": "^6.0.0",
"@sap/cloud-sdk-core": "^undefined",
"reflect-metadata": "^0.1.12",
"rimraf": "^2.6.2",
"rxjs": "^6.3.3",
},
"description": "",
"devDependencies": Object {
"@nestjs/testing": "6.1.1",
"@sap-cloud-sdk/cli": "^undefined",
"@sap/cloud-sdk-test-util": "^undefined",
"@types/express": "4.16.1",
"@types/jest": "24.0.11",
"@types/node": "11.13.4",
"@types/supertest": "2.0.7",
"jest": "24.7.1",
"prettier": "1.17.0",
"supertest": "4.0.2",
"ts-jest": "24.0.2",
"ts-node": "8.1.0",
"tsc-watch": "2.2.1",
"tsconfig-paths": "3.8.0",
"tslint": "5.16.0",
"typescript": "3.4.3",
},
"jest": Object {
"coverageDirectory": "../coverage",
"moduleFileExtensions": Array [
"js",
"json",
"ts",
],
"rootDir": "src",
"testEnvironment": "node",
"testRegex": ".spec.ts$",
"transform": Object {
"^.+\\\\.(t|j)s$": "ts-jest",
},
},
"license": "MIT",
"name": "nest",
"scripts": Object {
"build": "rimraf dist && tsc -p tsconfig.build.json",
"ci-backend-unit-test": "echo \\"Test your application and write results in a JUnit format to \`s4hana_pipeline/reports/backend-unit/\` and coverage in a cobertura format to \`s4hana_pipeline/reports/coverage/backend-unit/\`\\"",
"ci-build": "echo \\"Use this to compile or minify your application\\"",
"ci-integration-test": "echo \\"Test your application and write results in a JUnit format to \`s4hana_pipeline/reports/backend-integration/\` and coverage in a cobertura format to \`s4hana_pipeline/reports/coverage/backend-integration/\`\\"",
"ci-package": "sap-cloud-sdk package --include=\\"package.json,package-lock.json,index.js,dist/**/*\\"",
"format": "prettier --write \\"src/**/*.ts\\"",
"lint": "tslint -p tsconfig.json -c tslint.json",
"start": "ts-node -r tsconfig-paths/register src/main.ts",
"start:debug": "tsc-watch -p tsconfig.build.json --onSuccess \\"node --inspect-brk dist/main.js\\"",
"start:dev": "tsc-watch -p tsconfig.build.json --onSuccess \\"node dist/main.js\\"",
"start:prod": "node dist/main.js",
"test": "jest",
"test:cov": "jest --coverage",
"test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
"test:e2e": "jest --config ./test/jest-e2e.json",
"test:watch": "jest --watch",
},
"version": "0.0.1",
}
`;

exports[`Package Json Utils add scripts, dependencies and test config for scaffolded project 1`] = `
Object {
"author": "",
"dependencies": Object {
"@nestjs/common": "^6.0.0",
"@nestjs/core": "^6.0.0",
"@nestjs/platform-express": "^6.0.0",
"@sap/cloud-sdk-core": "^undefined",
"reflect-metadata": "^0.1.12",
"rimraf": "^2.6.2",
"rxjs": "^6.3.3",
},
"description": "",
"devDependencies": Object {
"@nestjs/testing": "6.1.1",
"@sap-cloud-sdk/cli": "^undefined",
"@sap/cloud-sdk-test-util": "^undefined",
"@types/express": "4.16.1",
"@types/jest": "24.0.11",
"@types/node": "11.13.4",
"@types/supertest": "2.0.7",
"jest": "24.7.1",
"jest-junit": "^undefined",
"prettier": "1.17.0",
"supertest": "4.0.2",
"ts-jest": "24.0.2",
"ts-node": "8.1.0",
"tsc-watch": "2.2.1",
"tsconfig-paths": "3.8.0",
"tslint": "5.16.0",
"typescript": "3.4.3",
},
"jest": Object {
"collectCoverage": true,
"coverageDirectory": "../s4hana_pipeline/reports/coverage-reports/backend-unit",
"coverageReporters": Array [
"text",
"cobertura",
],
"moduleFileExtensions": Array [
"js",
"json",
"ts",
],
"reporters": Array [
"default",
Array [
"jest-junit",
Object {
"outputDirectory": "./s4hana_pipeline/reports/backend-unit",
"suiteName": "backend unit tests",
},
],
],
"rootDir": "src",
"testEnvironment": "node",
"testRegex": ".spec.ts$",
"transform": Object {
"^.+\\\\.(t|j)s$": "ts-jest",
},
},
"license": "MIT",
"name": "nest",
"scripts": Object {
"build": "rimraf dist && tsc -p tsconfig.build.json",
"ci-backend-unit-test": "jest --ci",
"ci-build": "npm run build",
"ci-e2e": "echo \\"Test your application and write results in a JUnit format to \`s4hana_pipeline/reports/e2e/\`\\"",
"ci-frontend-unit-test": "echo \\"Test your application and write results in a JUnit format to \`s4hana_pipeline/reports/frontend-unit/\` and coverage in a cobertura format to \`s4hana_pipeline/reports/coverage/frontend-unit/\`\\"",
"ci-integration-test": "jest --ci --config ./test/jest-e2e.json",
"ci-package": "sap-cloud-sdk package",
"deploy": "npm run ci-build && npm run ci-package && cf push",
"format": "prettier --write \\"src/**/*.ts\\"",
"lint": "tslint -p tsconfig.json -c tslint.json",
"start": "ts-node -r tsconfig-paths/register src/main.ts",
"start:debug": "tsc-watch -p tsconfig.build.json --onSuccess \\"node --inspect-brk dist/main.js\\"",
"start:dev": "tsc-watch -p tsconfig.build.json --onSuccess \\"node dist/main.js\\"",
"start:prod": "node dist/main.js",
"test": "jest",
"test:cov": "jest --coverage",
"test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
"test:e2e": "jest --config ./test/jest-e2e.json",
"test:watch": "jest --watch",
},
"version": "0.0.1",
}
`;

exports[`Package Json Utils should parse the package.json 1`] = `
Array [
"author",
"dependencies",
"description",
"devDependencies",
"jest",
"license",
"name",
"scripts",
"version",
]
`;
17 changes: 17 additions & 0 deletions test/utils/__snapshots__/scaffold.spec.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Scaffold Utils should build the scaffold 1`] = `
Array [
".git",
".gitignore",
".prettierrc",
"README.md",
"nest-cli.json",
"package.json",
"src",
"test",
"tsconfig.build.json",
"tsconfig.json",
"tslint.json",
]
`;
40 changes: 40 additions & 0 deletions test/utils/__snapshots__/templates.spec.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Templates Utils should copy files locally 1`] = `
Array [
".npmrc",
"Jenkinsfile",
"credentials.json",
"deployment",
"manifest.yml",
"pipeline_config.yml",
"s4hana_pipeline",
"systems.json",
]
`;

exports[`Templates Utils should return information which files to copy where 1`] = `
Array [
".gitkeep",
".gitkeep",
".gitkeep",
".gitkeep",
".gitkeep",
".npmrc",
"Jenkinsfile",
"credentials.json",
"manifest.yml",
"pipeline_config.yml",
"systems.json",
]
`;

exports[`Templates Utils should return information which files to copy where 2`] = `
Array [
".npmrc",
"manifest.yml",
"package.json",
"xs-app.json",
"xs-security.json",
]
`;
77 changes: 77 additions & 0 deletions test/utils/git-ignore.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*!
* Copyright (c) 2019 SAP SE or an SAP affiliate company. All rights reserved.
*/
const log = jest.fn();
const warn = jest.fn(message => console.log('MOCKED WARNING: ', message));

jest.mock('cli-ux', () => {
// Mocking needs to happen before the command is imported
const cli = jest.requireActual('cli-ux');
return {
...cli,
default: {
...cli.default,
log,
warn
}
};
});

import * as fs from 'fs';
import * as path from 'path';
import * as rm from 'rimraf';
import { modifyGitIgnore } from '../../src/utils';

const pathPrefix = path.resolve(__dirname, __filename.replace(/\./g, '-')).replace('-ts', '');

function getCleanProjectDir(name: string) {
const projectDir = path.resolve(pathPrefix, name);
if (fs.existsSync(projectDir)) {
rm.sync(projectDir);
}
fs.mkdirSync(projectDir, { recursive: true });
return projectDir;
}

describe('Git Ignore Utils', () => {
afterAll(() => {
rm.sync(pathPrefix);
});

it('should add paths to empty git ignore', () => {
const projectDir = getCleanProjectDir('empty-git-ignore');
fs.writeFileSync(`${projectDir}/.gitignore`, '');

modifyGitIgnore(projectDir);

const gitIgnoreContent = fs.readFileSync(`${projectDir}/.gitignore`, { encoding: 'utf8' }).split('\n');
expect(gitIgnoreContent).toContain('/s4hana_pipeline');
expect(gitIgnoreContent).toContain('credentials.json');
});

it('should add paths to existing git ignore', () => {
const projectDir = getCleanProjectDir('existing-git-ignore');
fs.writeFileSync(
`${projectDir}/.gitignore`,
`myPath
foobar

!@#$%^&^
\\n`
);
modifyGitIgnore(projectDir);

const gitIgnoreContent = fs.readFileSync(`${projectDir}/.gitignore`, { encoding: 'utf8' }).split('\n');
expect(gitIgnoreContent).toContain('/s4hana_pipeline');
expect(gitIgnoreContent).toContain('credentials.json');
});

it('warn if there is no git ignore', () => {
const projectDir = getCleanProjectDir('no-git-ignore');

modifyGitIgnore(projectDir);

expect(warn).toHaveBeenCalledWith('No .gitignore file found!');
expect(log).toHaveBeenCalledTimes(4);
});
});
Loading