|
1 | 1 | // Note: some tests rely on side-effects from prior tests.
|
2 | 2 | // You may not get meaningful results running a subset of tests.
|
3 | 3 |
|
| 4 | +const Module = require('module'); |
4 | 5 | const priorErrorPrepareStackTrace = Error.prepareStackTrace;
|
5 | 6 | const priorProcessEmit = process.emit;
|
| 7 | +const priorResolveFilename = Module._resolveFilename; |
6 | 8 | const underTest = require('./source-map-support');
|
7 | 9 | var SourceMapGenerator = require('source-map').SourceMapGenerator;
|
8 | 10 | var child_process = require('child_process');
|
@@ -704,16 +706,52 @@ it('supports multiple instances', function(done) {
|
704 | 706 | ]);
|
705 | 707 | });
|
706 | 708 |
|
| 709 | +describe('redirects require() of "source-map-support" to this module', function() { |
| 710 | + it('redirects', function() { |
| 711 | + assert.strictEqual(require.resolve('source-map-support'), require.resolve('.')); |
| 712 | + assert.strictEqual(require.resolve('source-map-support/register'), require.resolve('./register')); |
| 713 | + assert.strictEqual(require('source-map-support'), require('.')); |
| 714 | + }); |
| 715 | + |
| 716 | + it('emits notifications', function() { |
| 717 | + let onConflictingLibraryRedirectCalls = []; |
| 718 | + let onConflictingLibraryRedirectCalls2 = []; |
| 719 | + underTest.install({ |
| 720 | + onConflictingLibraryRedirect(request, parent, isMain, redirectedRequest) { |
| 721 | + onConflictingLibraryRedirectCalls.push([...arguments]); |
| 722 | + } |
| 723 | + }); |
| 724 | + underTest.install({ |
| 725 | + onConflictingLibraryRedirect(request, parent, isMain, redirectedRequest) { |
| 726 | + onConflictingLibraryRedirectCalls2.push([...arguments]); |
| 727 | + } |
| 728 | + }); |
| 729 | + require.resolve('source-map-support'); |
| 730 | + assert.strictEqual(onConflictingLibraryRedirectCalls.length, 1); |
| 731 | + assert.strictEqual(onConflictingLibraryRedirectCalls2.length, 1); |
| 732 | + for(const args of [onConflictingLibraryRedirectCalls[0], onConflictingLibraryRedirectCalls2[0]]) { |
| 733 | + const [request, parent, isMain, options, redirectedRequest] = args; |
| 734 | + assert.strictEqual(request, 'source-map-support'); |
| 735 | + assert.strictEqual(parent, module); |
| 736 | + assert.strictEqual(isMain, false); |
| 737 | + assert.strictEqual(options, undefined); |
| 738 | + assert.strictEqual(redirectedRequest, require.resolve('.')); |
| 739 | + } |
| 740 | + }); |
| 741 | +}); |
| 742 | + |
707 | 743 | describe('uninstall', function() {
|
708 | 744 | this.beforeEach(function() {
|
709 | 745 | underTest.uninstall();
|
710 | 746 | process.emit = priorProcessEmit;
|
711 | 747 | Error.prepareStackTrace = priorErrorPrepareStackTrace;
|
| 748 | + Module._resolveFilename = priorResolveFilename; |
712 | 749 | });
|
713 | 750 |
|
714 | 751 | it('uninstall removes hooks and source-mapping behavior', function() {
|
715 | 752 | assert.strictEqual(Error.prepareStackTrace, priorErrorPrepareStackTrace);
|
716 | 753 | assert.strictEqual(process.emit, priorProcessEmit);
|
| 754 | + assert.strictEqual(Module._resolveFilename, priorResolveFilename); |
717 | 755 | normalThrowWithoutSourceMapSupportInstalled();
|
718 | 756 | });
|
719 | 757 |
|
@@ -773,4 +811,20 @@ describe('uninstall', function() {
|
773 | 811 | process.emit('foo');
|
774 | 812 | assert(peInvocations >= 1);
|
775 | 813 | });
|
| 814 | + |
| 815 | + it('uninstall preserves third-party module._resolveFilename hooks installed after us', function() { |
| 816 | + installSms(); |
| 817 | + const wrappedResolveFilename = Module._resolveFilename; |
| 818 | + let peInvocations = 0; |
| 819 | + function thirdPartyModuleResolveFilename() { |
| 820 | + peInvocations++; |
| 821 | + return wrappedResolveFilename.apply(this, arguments); |
| 822 | + } |
| 823 | + Module._resolveFilename = thirdPartyModuleResolveFilename; |
| 824 | + underTest.uninstall(); |
| 825 | + assert.strictEqual(Module._resolveFilename, thirdPartyModuleResolveFilename); |
| 826 | + normalThrowWithoutSourceMapSupportInstalled(); |
| 827 | + Module._resolveFilename('repl'); |
| 828 | + assert(peInvocations >= 1); |
| 829 | + }); |
776 | 830 | });
|
0 commit comments