Skip to content
This repository has been archived by the owner on Dec 7, 2021. It is now read-only.

Commit

Permalink
Convert tests to typescript (#925)
Browse files Browse the repository at this point in the history
* Convert unit tests to typescript.

* Convert integration tests to typescript.
  • Loading branch information
rictic committed Nov 15, 2017
1 parent 8b81ca8 commit 524a206
Show file tree
Hide file tree
Showing 151 changed files with 1,607 additions and 1,756 deletions.
2 changes: 0 additions & 2 deletions .eslintignore

This file was deleted.

1 change: 0 additions & 1 deletion .npmignore
@@ -1,5 +1,4 @@
.clang-format
.eslintignore
.github
.gitignore
.travis.yml
Expand Down
9 changes: 9 additions & 0 deletions custom_typings/vinyl-fs-fake.d.ts
@@ -0,0 +1,9 @@
declare module 'vinyl-fs-fake' {
import * as vinyl from 'vinyl-fs';
export interface File {
path: string;
contents: string;
}
export interface Options { cwdbase: boolean; }
export function src(files: File[], options?: Options);
}
5 changes: 5 additions & 0 deletions custom_typings/yeoman-assert.d.ts
@@ -0,0 +1,5 @@
declare module 'yeoman-assert' {
export function file(filenames: string[]);
export function fileContent(path: string, content: string);
export function noFile(filenames: string[]);
}
15 changes: 3 additions & 12 deletions gulpfile.js
Expand Up @@ -12,7 +12,6 @@
'use strict';

const depcheck = require('depcheck');
const eslint = require('gulp-eslint');
const fs = require('fs-extra');
const gulp = require('gulp');
const mergeStream = require('merge-stream');
Expand All @@ -24,7 +23,7 @@ const typescript = require('gulp-typescript');

const tsProject = typescript.createProject('tsconfig.json');

gulp.task('lint', ['tslint', 'eslint', 'depcheck']);
gulp.task('lint', ['tslint', 'depcheck']);

gulp.task('clean', (done) => {
fs.remove(path.join(__dirname, 'lib'), done);
Expand All @@ -45,7 +44,7 @@ gulp.task('compile', () => {
gulp.task(
'test',
['build'],
() => gulp.src('test/unit/**/*_test.js', {read: false}).pipe(mocha({
() => gulp.src('lib/test/unit/**/*_test.js', {read: false}).pipe(mocha({
ui: 'tdd',
reporter: 'spec',
})));
Expand All @@ -54,12 +53,11 @@ gulp.task(
'test:integration',
['build'],
() =>
gulp.src(['test/integration/**/*_test.js'], {read: false}).pipe(mocha({
gulp.src(['lib/test/integration/**/*_test.js'], {read: false}).pipe(mocha({
ui: 'tdd',
reporter: 'spec',
})));


gulp.task(
'tslint',
() => gulp.src('src/**/*.ts')
Expand All @@ -69,13 +67,6 @@ gulp.task(
}))
.pipe(tslint.report()));

gulp.task(
'eslint',
() => gulp.src('test/**/*_test.js')
.pipe(eslint())
.pipe(eslint.format())
.pipe(eslint.failAfterError()));

gulp.task('depcheck', () => {
return depcheck(__dirname, {
// "@types/*" dependencies are type declarations that are
Expand Down
27 changes: 27 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions package.json
Expand Up @@ -84,22 +84,25 @@
"devDependencies": {
"@polymer/tools-common": "^1.0.1",
"@types/fs-extra": "0.0.37",
"@types/mocha": "^2.2.44",
"@types/node": "=6.0.65",
"@types/sinon": "^4.0.0",
"@types/tmp": "^0.0.33",
"@types/yeoman-test": "^1.7.3",
"babili": "^0.0.12",
"chai": "^3.5.0",
"clang-format": "=1.0.50",
"depcheck": "^0.6.3",
"fs-extra": "^2.0.0",
"gulp": "^3.9.1",
"gulp-eslint": "^3.0.1",
"gulp-spawn-mocha": "^3.1.0",
"gulp-tslint": "^7.0.1",
"gulp-typescript": "^3.0.0",
"istanbul": "^0.4",
"memory-streams": "^0.1.0",
"mocha": "^3.0",
"run-sequence": "^1.2.0",
"sinon": "^1.17.4",
"sinon": "^1.17.7",
"tmp": "0.0.31",
"tslint": "^4.0.2",
"typescript": "^2.2.0",
Expand Down
3 changes: 1 addition & 2 deletions src/init/init.ts
Expand Up @@ -230,8 +230,7 @@ function createSelectPrompt(env: YeomanEnvironment): InquirerQuestion {
* error will be thrown.
*/
export async function runGenerator(
generatorName: string, options: {[name: string]: any}): Promise<void> {
options = options || {};
generatorName: string, options: {[name: string]: any} = {}): Promise<void> {
const templateName = options['templateName'] || generatorName;

const env: YeomanEnvironment =
Expand Down
173 changes: 173 additions & 0 deletions src/test/integration/build_test.ts
@@ -0,0 +1,173 @@
/*
* Copyright (c) 2017 The Polymer Project Authors. All rights reserved.
* This code may only be used under the BSD style license found at
* http://polymer.github.io/LICENSE.txt The complete set of authors may be found
* at http://polymer.github.io/AUTHORS.txt The complete set of contributors may
* be found at http://polymer.github.io/CONTRIBUTORS.txt Code distributed by
* Google as part of the polymer project is also subject to an additional IP
* rights grant found at http://polymer.github.io/PATENTS.txt
*/

'use strict';

import {assert} from 'chai';
import * as path from 'path';
import {fs} from 'mz';
import * as fsExtra from 'fs-extra';
import * as tmp from 'tmp';
import {runCommand} from './run-command';

const fixturePath =
path.join(__dirname, '../../../src/test/integration/fixtures/');

tmp.setGracefulCleanup();

suite('polymer build', function() {

const binPath = path.join(__dirname, '../../../bin/polymer.js');

this.timeout(5 * 1000);

test('handles a simple correct case', async () => {
const tmpDir = tmp.dirSync();
copyDir(path.join(fixturePath, 'build-simple', 'source'), tmpDir.name);

await runCommand(binPath, ['build'], {cwd: tmpDir.name});
assertDirsEqual(
path.join(tmpDir.name, 'build'),
path.join(fixturePath, 'build-simple', 'expected'));
});

test('handles a CLI preset', async () => {
const tmpDir = tmp.dirSync();
copyDir(path.join(fixturePath, 'build-with-preset', 'source'), tmpDir.name);

await runCommand(binPath, ['build', '--preset', 'es5-bundled'], {
cwd: tmpDir.name,
});
assertDirsEqual(
path.join(tmpDir.name, 'build'),
path.join(fixturePath, 'build-with-preset', 'expected'));
});

test('handles equivalent of the CLI preset', async () => {
const tmpDir = tmp.dirSync();
copyDir(path.join(fixturePath, 'build-with-preset', 'source'), tmpDir.name);

await runCommand(
binPath,
[
'build',
'--add-push-manifest',
'--add-service-worker',
'--bundle',
'--css-minify',
'--html-minify',
'--js-compile',
'--js-minify'
],
{cwd: tmpDir.name});
assertDirsEqual(
path.join(tmpDir.name, 'build/default'),
path.join(fixturePath, 'build-with-preset', 'expected/es5-bundled'));
});

test('handled (default) bundle all into the entrypoint', async () => {
const tmpDir = tmp.dirSync();
copyDir(
path.join(fixturePath, 'fragment-variations', 'source'), tmpDir.name);

await runCommand(binPath, ['build', '--bundle'], {
cwd: tmpDir.name,
});
assertDirsEqual(
path.join(tmpDir.name, 'build/default'),
path.join(
fixturePath, 'fragment-variations', 'expected-default', 'default'));
});

test('handled bundle into fragment a', async () => {
const tmpDir = tmp.dirSync();
copyDir(
path.join(fixturePath, 'fragment-variations', 'source'), tmpDir.name);

await runCommand(binPath, ['build', '--bundle', '--fragment', 'a.html'], {
cwd: tmpDir.name,
});
assertDirsEqual(
path.join(tmpDir.name, 'build/default'),
path.join(
fixturePath,
'fragment-variations',
'expected-fragment-a',
'default'));
});

test('handled bundle into fragment a and b', async () => {
const tmpDir = tmp.dirSync();
copyDir(
path.join(fixturePath, 'fragment-variations', 'source'), tmpDir.name);

await runCommand(
binPath,
['build', '--bundle', '--fragment', 'a.html', '--fragment', 'b.html'],
{cwd: tmpDir.name});

assertDirsEqual(
path.join(tmpDir.name, 'build/default'),
path.join(
fixturePath,
'fragment-variations',
'expected-fragment-b',
'default'));
});

test('handles polymer 1.x project bundler defaults', async () => {
const tmpDir = tmp.dirSync();
copyDir(path.join(fixturePath, 'polymer-1-project', 'source'), tmpDir.name);

await runCommand(binPath, ['build', '--bundle'], {cwd: tmpDir.name});

assertDirsEqual(
path.join(tmpDir.name, 'build/default'),
path.join(fixturePath, 'polymer-1-project', 'expected/default'));
});

test('handles polymer 2.x project bundler defaults', async () => {
const tmpDir = tmp.dirSync();
copyDir(path.join(fixturePath, 'polymer-2-project', 'source'), tmpDir.name);

await runCommand(binPath, ['build', '--bundle'], {cwd: tmpDir.name});
assertDirsEqual(
path.join(tmpDir.name, 'build/default'),
path.join(fixturePath, 'polymer-2-project', 'expected/default'));
});
});

function copyDir(fromDir: string, toDir: string) {
fsExtra.copy(fromDir, toDir);
}

function assertDirsEqual(actual: string, expected: string, basedir = actual) {
const actualNames = fs.readdirSync(actual).sort();
const expectedNames = fs.readdirSync(expected).sort();
assert.deepEqual(
actualNames,
expectedNames,
`expected files in directory ${path.relative(basedir, actual)}`);
for (const fn of actualNames) {
const subActual = path.join(actual, fn);
const subExpected = path.join(expected, fn);
const stat = fs.statSync(subActual);
if (stat.isDirectory()) {
assertDirsEqual(subActual, subExpected, basedir);
} else {
const actualContents = fs.readFileSync(subActual, 'utf-8').trim();
const expectedContents = fs.readFileSync(subExpected, 'utf-8').trim();
assert.deepEqual(
actualContents,
expectedContents,
`expected contents of ${path.relative(basedir, subActual)}`);
}
}
}

0 comments on commit 524a206

Please sign in to comment.