Skip to content

Commit

Permalink
Add test for TypeScript with mixed import syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
pahen committed Nov 11, 2019
1 parent f1125d0 commit 50c1c10
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 52 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
*.sublime-project
*.sublime-workspace
.idea
package-lock.json
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -330,10 +330,11 @@ $ npm test

It could happen that the files you're not seeing have been skipped due to errors or that they can't be resolved. Run madge with the `--warning` option to see skipped files. If you need even more info run with the `--debug` option.

## Using both CommonJS and ES6 imports in same file?
## Using mixed import syntax in the same file?

Only one syntax is used by default. You can use both though if you're willing to take the degraded performance. Put this in your madge config to enable mixed imports.

For ES6 + CommonJS:
```json
{
"detectiveOptions": {
Expand All @@ -344,6 +345,17 @@ Only one syntax is used by default. You can use both though if you're willing to
}
```

For TypeScript + CommonJS:
```json
{
"detectiveOptions": {
"ts": {
"mixedImports": true
}
}
}
```

## How to ignore `import type` statements in ES6 + Flow?

Put this in your madge config.
Expand Down
32 changes: 0 additions & 32 deletions test/__snapshots__/api.js.snap

This file was deleted.

27 changes: 8 additions & 19 deletions test/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ const os = require('os');
const path = require('path');
const fs = require('mz/fs');
const madge = require('../lib/api');
const expect = require('expect');
const toMatchSnapshot = require('expect-mocha-snapshot');
expect.extend({toMatchSnapshot});

require('should');

Expand Down Expand Up @@ -209,22 +206,14 @@ describe('API', () => {
});

describe('dot()', () => {
it('returns a promise resolved with graphviz DOT output', async function () {
const res = await madge(__dirname + '/cjs/b.js', {
fontSize: '0',
// Custom config to make test pass on different OS
graphVizOptions: {
G: {
layout: 'neato',
overlap: true
},
N: {
pos: '0,0!',
height: 1
}
}
});
expect(await res.dot()).toMatchSnapshot(this);
it('returns a promise resolved with graphviz DOT output', async () => {
const res = await madge(__dirname + '/cjs/b.js');
const output = await res.dot();
output.should.match(/digraph G/);
output.should.match(/bgcolor="#111111"/);
output.should.match(/fontcolor="#c6c5fe"/);
output.should.match(/color="#757575"/);
output.should.match(/fontcolor="#cfffac"/);
});
});

Expand Down
11 changes: 11 additions & 0 deletions test/typescript.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,15 @@ describe('TypeScript', () => {
done();
}).catch(done);
});

it('supports CJS modules when using mixedImports option', (done) => {
madge(dir + '/mixed.ts', {detectiveOptions: {ts: {mixedImports: true}}}).then((res) => {
res.obj().should.eql({
'export-x.tsx': [],
'export.ts': [],
'mixed.ts': ['export-x.tsx', 'export.ts']
});
done();
}).catch(done);
});
});
2 changes: 2 additions & 0 deletions test/typescript/mixed.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
require('./export');
import './export-x';

0 comments on commit 50c1c10

Please sign in to comment.