Skip to content

Commit

Permalink
test: minor cleanup (#170)
Browse files Browse the repository at this point in the history
  • Loading branch information
ofrobots committed Jun 8, 2018
1 parent 9a1bfdd commit a34ca31
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 82 deletions.
44 changes: 19 additions & 25 deletions test/test-clean.ts
Expand Up @@ -33,47 +33,41 @@ const OPTIONS: Options = {
logger: {log: nop, error: nop, dir: nop}
};

test.failing.serial(
'should gracefully error if tsconfig is missing', async t => {
await withFixtures({}, async () => {
await clean(OPTIONS);
});
});
test.failing.serial('should gracefully error if tsconfig is missing', t => {
return withFixtures({}, async () => {
await clean(OPTIONS);
});
});

test.serial(
'should gracefully error if tsconfig does not have valid outDir',
async t => {
await withFixtures({'tsconfig.json': JSON.stringify({})}, async () => {
'should gracefully error if tsconfig does not have valid outDir', t => {
return withFixtures({'tsconfig.json': JSON.stringify({})}, async () => {
const deleted = await clean(OPTIONS);
t.is(deleted, false);
});
});

test.serial('should avoid deleting .', async t => {
await withFixtures(
test.serial('should avoid deleting .', t => {
return withFixtures(
{'tsconfig.json': JSON.stringify({compilerOptions: {outDir: '.'}})},
async () => {
const deleted = await clean(OPTIONS);
t.is(deleted, false);
});
});

test.failing.serial(
'should ensure that outDir is local to targetRoot', async t => {
await withFixtures(
{
'tsconfig.json':
JSON.stringify({compilerOptions: {outDir: '../out'}})
},
async () => {
const deleted = await clean(OPTIONS);
t.is(deleted, false);
});
});
test.failing.serial('should ensure that outDir is local to targetRoot', t => {
return withFixtures(
{'tsconfig.json': JSON.stringify({compilerOptions: {outDir: '../out'}})},
async () => {
const deleted = await clean(OPTIONS);
t.is(deleted, false);
});
});

test.serial('should remove outDir', async t => {
test.serial('should remove outDir', t => {
const OUT = 'outputDirectory';
await withFixtures(
return withFixtures(
{
'tsconfig.json': JSON.stringify({compilerOptions: {outDir: OUT}}),
[OUT]: {}
Expand Down
8 changes: 4 additions & 4 deletions test/test-format.ts
Expand Up @@ -35,17 +35,17 @@ const OPTIONS: Options = {
logger: {log: console.log, error: console.error, dir: nop}
};

test.serial('format should return false for ill-formatted files', async t => {
await withFixtures(
test.serial('format should return false for ill-formatted files', t => {
return withFixtures(
{'tsconfig.json': JSON.stringify({files: ['a.ts']}), 'a.ts': BAD_CODE},
async () => {
const result = await format.format(OPTIONS, [], false);
t.false(result);
});
});

test.serial('format should only look in root files', async t => {
await withFixtures(
test.serial('format should only look in root files', t => {
return withFixtures(
{
'tsconfig.json': JSON.stringify({files: ['a.ts']}),
'a.ts': 'import {foo} from \'./b\';\n',
Expand Down
102 changes: 49 additions & 53 deletions test/test-lint.ts
Expand Up @@ -40,15 +40,15 @@ const GOOD_CODE = `throw new Error('hello world');`;
const FIXABLE_CODE = 'const x : Array<string> = []';
const FIXABLE_CODE_FIXED = 'const x : string[] = [];';

test.serial('createProgram should return an object', async t => {
await withFixtures({'tsconfig.json': '{}'}, async () => {
test.serial('createProgram should return an object', t => {
return withFixtures({'tsconfig.json': '{}'}, async () => {
const program = lint.createProgram(OPTIONS);
t.truthy(program);
});
});

test.serial('lint should return true on good code', async t => {
await withFixtures(
test.serial('lint should return true on good code', t => {
return withFixtures(
{
'tsconfig.json': JSON.stringify({files: ['a.ts']}),
'a.ts': GOOD_CODE,
Expand All @@ -59,8 +59,8 @@ test.serial('lint should return true on good code', async t => {
});
});

test.serial('lint should return false on bad code', async t => {
await withFixtures(
test.serial('lint should return false on bad code', t => {
return withFixtures(
{
'tsconfig.json': JSON.stringify({files: ['a.ts']}),
'a.ts': BAD_CODE,
Expand All @@ -71,8 +71,8 @@ test.serial('lint should return false on bad code', async t => {
});
});

test.serial('lint should auto fix fixable errors', async t => {
await withFixtures(
test.serial('lint should auto fix fixable errors', t => {
return withFixtures(
{
'tsconfig.json': JSON.stringify({files: ['a.ts']}),
'a.ts': FIXABLE_CODE
Expand All @@ -86,8 +86,8 @@ test.serial('lint should auto fix fixable errors', async t => {
});
});

test.serial('lint should not auto fix on dry-run', async t => {
await withFixtures(
test.serial('lint should not auto fix on dry-run', t => {
return withFixtures(
{
'tsconfig.json': JSON.stringify({files: ['a.ts']}),
'a.ts': FIXABLE_CODE
Expand All @@ -102,8 +102,8 @@ test.serial('lint should not auto fix on dry-run', async t => {
});
});

test.serial('lint should lint files listed in tsconfig.files', async t => {
await withFixtures(
test.serial('lint should lint files listed in tsconfig.files', t => {
return withFixtures(
{
'tsconfig.json': JSON.stringify({files: ['a.ts']}),
'a.ts': GOOD_CODE,
Expand All @@ -118,7 +118,7 @@ test.serial('lint should lint files listed in tsconfig.files', async t => {
test.serial(
'lint should lint *.ts files when no files or inlcude has been specified',
async t => {
await withFixtures(
return withFixtures(
{
'tsconfig.json': JSON.stringify({}),
'a.ts': GOOD_CODE,
Expand All @@ -133,7 +133,7 @@ test.serial(
test.serial(
'lint should lint files listed in tsconfig.files when empty list is provided',
async t => {
await withFixtures(
return withFixtures(
{
'tsconfig.json': JSON.stringify({files: ['a.ts']}),
'a.ts': FIXABLE_CODE,
Expand All @@ -149,8 +149,8 @@ test.serial(
});


test.serial('lint should not lint files listed in exclude', async t => {
await withFixtures(
test.serial('lint should not lint files listed in exclude', t => {
return withFixtures(
{
'tsconfig.json': JSON.stringify({exclude: ['b.*']}),
'a.ts': GOOD_CODE,
Expand All @@ -162,8 +162,8 @@ test.serial('lint should not lint files listed in exclude', async t => {
});
});

test.serial('lint should lint globs listed in include', async t => {
await withFixtures(
test.serial('lint should lint globs listed in include', t => {
return withFixtures(
{
'tsconfig.json': JSON.stringify({include: ['dirb/*']}),
dira: {'a.ts': GOOD_CODE},
Expand All @@ -175,8 +175,8 @@ test.serial('lint should lint globs listed in include', async t => {
});
});

test.serial('lint should lint only specified files', async t => {
await withFixtures(
test.serial('lint should lint only specified files', t => {
return withFixtures(
{
'tsconfig.json': JSON.stringify({}),
dira: {'a.ts': GOOD_CODE},
Expand All @@ -190,8 +190,8 @@ test.serial('lint should lint only specified files', async t => {
});
});

test.serial('lint should throw for unrecognized files', async t => {
await withFixtures(
test.serial('lint should throw for unrecognized files', t => {
return withFixtures(
{
'tsconfig.json': JSON.stringify({}),
'a.ts': GOOD_CODE,
Expand All @@ -218,7 +218,7 @@ test.serial('lint should prefer user config file over default', async t => {
});

// User should be able to override the default config.
await withFixtures(
return withFixtures(
{
'tsconfig.json': JSON.stringify({files: ['a.ts']}),
'tslint.json': JSON.stringify({}),
Expand All @@ -230,36 +230,32 @@ test.serial('lint should prefer user config file over default', async t => {
});
});

test.serial(
'lint for specific files should use file-specific config', async t => {
const CODE_WITH_PARSEINT = 'parseInt(42);';
let logBuffer = '';
const optionsWithLog = Object.assign({}, OPTIONS, {
logger: {
log: (...args: string[]) => {
logBuffer += (args.join(' '));
},
error: nop,
dir: nop
}
test.serial('lint for specific files should use file-specific config', t => {
const CODE_WITH_PARSEINT = 'parseInt(42);';
let logBuffer = '';
const optionsWithLog = Object.assign({}, OPTIONS, {
logger: {
log: (...args: string[]) => {
logBuffer += (args.join(' '));
},
error: nop,
dir: nop
}
});
return withFixtures(
{
dira: {
'a.ts': CODE_WITH_PARSEINT,
// no tslint, so default should apply.
},
dirb: {'b.ts': CODE_WITH_PARSEINT, 'tslint.json': JSON.stringify({})}
},
async () => {
const okay = lint.lint(optionsWithLog, ['dira/a.ts', 'dirb/b.ts']);
t.false(okay);
t.regex(logBuffer, /dira\/a\.ts/);
t.notRegex(logBuffer, /dirb\/b\.ts/);
});
await withFixtures(
{
dira: {
'a.ts': CODE_WITH_PARSEINT,
// no tslint, so default should apply.
},
dirb: {
'b.ts': CODE_WITH_PARSEINT,
'tslint.json': JSON.stringify({})
}
},
async () => {
const okay = lint.lint(optionsWithLog, ['dira/a.ts', 'dirb/b.ts']);
t.false(okay);
t.regex(logBuffer, /dira\/a\.ts/);
t.notRegex(logBuffer, /dirb\/b\.ts/);
});
});
});

// TODO: test for when tsconfig.json is missing.

0 comments on commit a34ca31

Please sign in to comment.