Skip to content

Commit

Permalink
test: ✅ fix test scenarios
Browse files Browse the repository at this point in the history
include `cosmiconfig` APIs to perform testing

fix EndemolShineGroup#134
  • Loading branch information
achmadk committed Feb 22, 2021
1 parent b4807f9 commit 92e2cf5
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 19 deletions.
7 changes: 7 additions & 0 deletions src/__fixtures__/error-interface-mismatch.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Example } from './success'

const errorExample: Example = {
foo: 1
}

export default errorExample
3 changes: 0 additions & 3 deletions src/__fixtures__/error.ts

This file was deleted.

12 changes: 9 additions & 3 deletions src/__fixtures__/success.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
export default {
foo: 'bar',
};
export interface Example {
foo: string
}

const example: Example = {
foo: 'bar'
}

export default example;
5 changes: 5 additions & 0 deletions src/__fixtures__/test-error.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const errorAttributeNoValue = {
foo:,
}

export default errorAttributeNoValue;
9 changes: 9 additions & 0 deletions src/__fixtures__/test-success.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
interface TestConfig {
foo: string
}

const test: TestConfig = {
foo: 'bar'
}

export default test
95 changes: 82 additions & 13 deletions src/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,91 @@
import path from 'path';
import { cosmiconfig, cosmiconfigSync } from 'cosmiconfig';

import loader from '.';
import TypeScriptCompileError from './Errors/TypeScriptCompileError';

const FIXTURES_PATH = path.resolve(__dirname, '__fixtures__');

describe('TypeScriptLoader', () => {
it('compiles a valid TypeScript file', async () => {
const result = await loader(path.resolve(FIXTURES_PATH, 'success'), '');
expect(result).toEqual({ foo: 'bar' });
});

it('fails to compile an invalid TypeScript file', async () => {
try {
await loader(path.resolve(FIXTURES_PATH, 'error'), '');
} catch (error) {
expect(error).toBeInstanceOf(TypeScriptCompileError);
expect(error.toObject().message).toMatch('Failed to compile TypeScript');
}
});
describe('via cosmiconfig API', () => {
it('compiles a valid TypeScript file', async () => {
try {
const test = await cosmiconfig('test-success', {
loaders: {
'.ts': loader()
}
}).load(path.resolve(FIXTURES_PATH, 'test-success.config.ts'))
expect(test?.config).toEqual({ foo: 'bar' });
} catch (error) {
throw error
}
});

it('fails to compile an invalid TypeScript file because no value inside object', async () => {
try {
const test = await cosmiconfig('test-error', {
loaders: {
'.ts': loader()
}
}).load(path.resolve(FIXTURES_PATH, 'test-error.config.ts'))
expect(test?.config).toEqual({ foo: 'bar' });
} catch (error) {
expect(error).toBeInstanceOf(TypeScriptCompileError);
expect(error.toObject().message).toMatch('Failed to compile TypeScript');
}
});

it('fails to compile an invalid TypeScript file because interface mismatch', async () => {
try {
const test = await cosmiconfig('error-interface-mismatch', {
loaders: {
'.ts': loader()
}
}).load(path.resolve(FIXTURES_PATH, 'error-interface-mismatch.config.ts'))
expect(test?.config).toEqual({ foo: 'bar' });
} catch (error) {
expect(error).toBeInstanceOf(TypeScriptCompileError);
expect(error.toObject().message).toMatch('Failed to compile TypeScript');
}
});
})

describe('via cosmiconfigSync API', () => {
it('compiles a valid TypeScript file', () => {
const test = cosmiconfigSync('test-success', {
loaders: {
'.ts': loader()
}
}).load(path.resolve(FIXTURES_PATH, 'test-success.config.ts'))
expect(test?.config).toEqual({ foo: 'bar' });
});

it('fails to compile an invalid TypeScript file because no value inside object', () => {
try {
const test = cosmiconfigSync('test-error', {
loaders: {
'.ts': loader()
}
}).load(path.resolve(FIXTURES_PATH, 'test-error.config.ts'))
expect(test?.config).toEqual({ foo: 'bar' });
} catch (error) {
expect(error).toBeInstanceOf(TypeScriptCompileError);
expect(error.toObject().message).toMatch('Failed to compile TypeScript');
}
});

it('fails to compile an invalid TypeScript file because interface mismatch', () => {
try {
const test = cosmiconfigSync('error-interface-mismatch', {
loaders: {
'.ts': loader()
}
}).load(path.resolve(FIXTURES_PATH, 'error-interface-mismatch.config.ts'))
expect(test?.config).toEqual({ foo: 'bar' });
} catch (error) {
expect(error).toBeInstanceOf(TypeScriptCompileError);
expect(error.toObject().message).toMatch('Failed to compile TypeScript');
}
});
})
});

0 comments on commit 92e2cf5

Please sign in to comment.