Thanks to visit codestin.com
Credit goes to github.com

Skip to content

test(typescript): test added for issue #735 #736

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
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
133 changes: 84 additions & 49 deletions packages/typescript/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -249,19 +249,29 @@ test.serial('ensures multiple outputs can be built', async (t) => {
plugins: [typescript({ tsconfig: 'fixtures/multiple-files/tsconfig.json' })]
});

const output1 = await getCode(bundle1, { file: 'fixtures/multiple-files/index.js', format: 'cjs' }, true);
const output1 = await getCode(
bundle1,
{ file: 'fixtures/multiple-files/index.js', format: 'cjs' },
true
);

const bundle2 = await rollup({
input: 'fixtures/multiple-files/src/server.ts',
plugins: [typescript({ tsconfig: 'fixtures/multiple-files/tsconfig.json' })]
});

const output2 = await getCode(bundle2, { file: 'fixtures/multiple-files/server.js', format: 'cjs' }, true);

t.deepEqual(
[...new Set(output1.concat(output2).map((out) => out.fileName))].sort(),
['index.d.ts', 'index.js', 'server.d.ts', 'server.js']
const output2 = await getCode(
bundle2,
{ file: 'fixtures/multiple-files/server.js', format: 'cjs' },
true
);

t.deepEqual([...new Set(output1.concat(output2).map((out) => out.fileName))].sort(), [
'index.d.ts',
'index.js',
'server.d.ts',
'server.js'
]);
});

test.serial('relative paths in tsconfig.json are resolved relative to the file', async (t) => {
Expand Down Expand Up @@ -1088,50 +1098,48 @@ test('supports custom transformers', async (t) => {
});

function fakeTypescript(custom) {
return Object.assign(
{
sys: ts.sys,
createModuleResolutionCache: ts.createModuleResolutionCache,
ModuleKind: ts.ModuleKind,

transpileModule() {
return {
outputText: '',
diagnostics: [],
sourceMapText: JSON.stringify({ mappings: '' })
};
},

createWatchCompilerHost() {
return {
afterProgramCreate() {}
};
},

createWatchProgram() {
return {};
},

parseJsonConfigFileContent(json, host, basePath, existingOptions) {
return {
options: {
...json.compilerOptions,
...existingOptions
},
fileNames: [],
errors: []
};
},

getOutputFileNames(_, id) {
return [id.replace(/\.tsx?/, '.js')];
},

// eslint-disable-next-line no-undefined
getTsBuildInfoEmitOutputFilePath: () => undefined
return {
sys: ts.sys,
createModuleResolutionCache: ts.createModuleResolutionCache,
ModuleKind: ts.ModuleKind,

transpileModule() {
return {
outputText: '',
diagnostics: [],
sourceMapText: JSON.stringify({ mappings: '' })
};
},
custom
);

createWatchCompilerHost() {
return {
afterProgramCreate() {}
};
},

createWatchProgram() {
return {};
},

parseJsonConfigFileContent(json, host, basePath, existingOptions) {
return {
options: {
...json.compilerOptions,
...existingOptions
},
fileNames: [],
errors: []
};
},

getOutputFileNames(_, id) {
return [id.replace(/\.tsx?/, '.js')];
},

// eslint-disable-next-line no-undefined
getTsBuildInfoEmitOutputFilePath: () => undefined,
...custom
};
}

test.serial('picks up on newly included typescript files in watch mode', async (t) => {
Expand Down Expand Up @@ -1209,6 +1217,33 @@ test.serial.skip('works when code is in src directory', async (t) => {
);
});

test.serial.skip(
'Should support creating declaration files when not using tsconfig.json',
async (t) => {
const bundle = await rollup({
input: 'fixtures/basic/main.ts',
plugins: [
typescript({
include: 'fixtures/basic/*.ts',
exclude: 'fixtures/basic/dist/types',
tsconfig: false,
declaration: true,
declarationDir: 'fixtures/basic/dist/types'
})
],
onwarn
});
const output = await getCode(bundle, { format: 'esm', dir: 'fixtures/basic/dist' }, true);

t.deepEqual(
output.map((out) => out.fileName),
['main.js', 'types/main.d.ts']
);

t.true(output[1].source.includes('declare const answer = 42;'), output[1].source);
}
);

function waitForWatcherEvent(watcher, eventCode) {
return new Promise((resolve, reject) => {
watcher.on('event', function handleEvent(event) {
Expand Down