如何测试不应导出的功能?

时间:2018-07-31 19:14:43

标签: angular typescript unit-testing

我正在研究一个有角度的项目,并且一直在为某些功能编写一些测试用例。不幸的是,由于某种原因,我碰壁了-我有一个应该导出但仍需要测试的功能。

据我了解,似乎可以使用rewire,但是我的所有尝试都在编写抱怨Can't resolve 'fs' in node_modules/globCan't resolve 'module的错误时产生大量错误。 'node_modules / rewire / lib'。

我不确定我做对了吗。这是我的代码:

// my-function.ts
function myFunctionPrivate() { }
export function myFunctionA() { }

// my-function.spec.ts
import * as rewire from 'rewire';

describe('Test myFunction', () => {
    it('should work', () => {
        const app = rewire('./my-function');

        const myFunctionPrivate = app.__get__('myFunctionPrivate');

        console.log(myFunctionPrivate)
    })
})

通过将其添加到package.json中,我可以解决一些错误:

"browser": {
  "fs": false,
  "path": false,
  "os": false
}

但是我仍然遇到错误:

WARNING in ./node_modules/eslint/lib/config/plugins.js
101:25-42 Critical dependency: the request of a dependency is an expression
 @ ./node_modules/eslint/lib/config/plugins.js
 @ ./node_modules/eslint/lib/config.js
 @ ./node_modules/eslint/lib/cli-engine.js
 @ ./node_modules/eslint/lib/api.js
 @ ./node_modules/rewire/lib/moduleEnv.js
 @ ./node_modules/rewire/lib/rewire.js
 @ ./node_modules/rewire/lib/index.js
 @ ./my-function.spec.ts
 @ ./src sync \.spec\.ts$
 @ ./src/test.ts

WARNING in ./node_modules/eslint/lib/config/plugins.js
106:20-45 Critical dependency: the request of a dependency is an expression
 @ ./node_modules/eslint/lib/config/plugins.js
 @ ./node_modules/eslint/lib/config.js
 @ ./node_modules/eslint/lib/cli-engine.js
 @ ./node_modules/eslint/lib/api.js
 @ ./node_modules/rewire/lib/moduleEnv.js
 @ ./node_modules/rewire/lib/rewire.js
 @ ./node_modules/rewire/lib/index.js
 @ ./my-function.spec.ts
 @ ./src sync \.spec\.ts$
 @ ./src/test.ts

WARNING in ./node_modules/eslint/lib/rules.js
118:33-61 Critical dependency: the request of a dependency is an expression
 @ ./node_modules/eslint/lib/rules.js
 @ ./node_modules/eslint/lib/linter.js
 @ ./node_modules/eslint/lib/api.js
 @ ./node_modules/rewire/lib/moduleEnv.js
 @ ./node_modules/rewire/lib/rewire.js
 @ ./node_modules/rewire/lib/index.js
 @ ./my-function.spec.ts
 @ ./src sync \.spec\.ts$
 @ ./src/test.ts

WARNING in ./node_modules/is-resolvable/index.js
11:2-9 Critical dependency: require function is used in a way in which dependencies cannot be statically extracted
 @ ./node_modules/is-resolvable/index.js
 @ ./node_modules/eslint/lib/config.js
 @ ./node_modules/eslint/lib/cli-engine.js
 @ ./node_modules/eslint/lib/api.js
 @ ./node_modules/rewire/lib/moduleEnv.js
 @ ./node_modules/rewire/lib/rewire.js
 @ ./node_modules/rewire/lib/index.js
 @ ./my-function.spec.ts
 @ ./src sync \.spec\.ts$
 @ ./src/test.ts

WARNING in ./node_modules/eslint/lib/config/plugins.js
125:37-62 Critical dependency: the request of a dependency is an expression
 @ ./node_modules/eslint/lib/config/plugins.js
 @ ./node_modules/eslint/lib/config.js
 @ ./node_modules/eslint/lib/cli-engine.js
 @ ./node_modules/eslint/lib/api.js
 @ ./node_modules/rewire/lib/moduleEnv.js
 @ ./node_modules/rewire/lib/rewire.js
 @ ./node_modules/rewire/lib/index.js
 @ ./my-function.spec.ts
 @ ./src sync \.spec\.ts$
 @ ./src/test.ts

WARNING in ./node_modules/require-uncached/index.js
28:8-25 Critical dependency: the request of a dependency is an expression
 @ ./node_modules/require-uncached/index.js
 @ ./node_modules/eslint/lib/config/config-file.js
 @ ./node_modules/eslint/lib/config.js
 @ ./node_modules/eslint/lib/cli-engine.js
 @ ./node_modules/eslint/lib/api.js
 @ ./node_modules/rewire/lib/moduleEnv.js
 @ ./node_modules/rewire/lib/rewire.js
 @ ./node_modules/rewire/lib/index.js
 @ ./my-function.spec.ts
 @ ./src sync \.spec\.ts$
 @ ./src/test.ts

WARNING in ./node_modules/eslint/lib/linter.js
540:46-65 Critical dependency: the request of a dependency is an expression
 @ ./node_modules/eslint/lib/linter.js
 @ ./node_modules/eslint/lib/api.js
 @ ./node_modules/rewire/lib/moduleEnv.js
 @ ./node_modules/rewire/lib/rewire.js
 @ ./node_modules/rewire/lib/index.js
 @ ./my-function.spec.ts
 @ ./src sync \.spec\.ts$
 @ ./src/test.ts

WARNING in ./node_modules/eslint/lib/cli-engine.js
705:23-45 Critical dependency: the request of a dependency is an expression
 @ ./node_modules/eslint/lib/cli-engine.js
 @ ./node_modules/eslint/lib/api.js
 @ ./node_modules/rewire/lib/moduleEnv.js
 @ ./node_modules/rewire/lib/rewire.js
 @ ./node_modules/rewire/lib/index.js
 @ ./my-function.spec.ts
 @ ./src sync \.spec\.ts$
 @ ./src/test.ts

WARNING in ./node_modules/rewire/lib/moduleEnv.js
Module not found: Error: Can't resolve 'coffee-script' in './node_modules/rewire/lib'
 @ ./node_modules/rewire/lib/moduleEnv.js 168:17-41
 @ ./node_modules/rewire/lib/rewire.js
 @ ./node_modules/rewire/lib/index.js
 @ ./my-function.spec.ts
 @ ./src sync \.spec\.ts$
 @ ./src/test.ts

WARNING in ./node_modules/rewire/lib/moduleEnv.js
Module not found: Error: Can't resolve 'coffeescript' in './node_modules/rewire/lib'
 @ ./node_modules/rewire/lib/moduleEnv.js 164:13-36
 @ ./node_modules/rewire/lib/rewire.js
 @ ./node_modules/rewire/lib/index.js
 @ ./my-function.spec.ts
 @ ./src sync \.spec\.ts$
 @ ./src/test.ts

WARNING in ./node_modules/rewire/lib/moduleEnv.js
require.extensions is not supported by webpack. Use a loader instead.
 @ ./node_modules/rewire/lib/rewire.js 6:16-41
 @ ./node_modules/rewire/lib/index.js
 @ ./my-function.spec.ts
 @ ./src sync \.spec\.ts$
 @ ./src/test.ts

WARNING in ./node_modules/rewire/lib/moduleEnv.js
require.extensions is not supported by webpack. Use a loader instead.
 @ ./node_modules/rewire/lib/rewire.js 6:16-41
 @ ./node_modules/rewire/lib/index.js
 @ ./my-function.spec.ts
 @ ./src sync \.spec\.ts$
 @ ./src/test.ts

WARNING in ./node_modules/rewire/lib/moduleEnv.js
require.extensions is not supported by webpack. Use a loader instead.
 @ ./node_modules/rewire/lib/rewire.js 6:16-41
 @ ./node_modules/rewire/lib/index.js
 @ ./my-function.spec.ts
 @ ./src sync \.spec\.ts$
 @ ./src/test.ts

WARNING in ./node_modules/rewire/lib/moduleEnv.js
require.extensions is not supported by webpack. Use a loader instead.
 @ ./node_modules/rewire/lib/rewire.js 6:16-41
 @ ./node_modules/rewire/lib/index.js
 @ ./my-function.spec.ts
 @ ./src sync \.spec\.ts$
 @ ./src/test.ts

WARNING in ./node_modules/rewire/lib/moduleEnv.js
require.extensions is not supported by webpack. Use a loader instead.
 @ ./node_modules/rewire/lib/rewire.js 6:16-41
 @ ./node_modules/rewire/lib/index.js
 @ ./my-function.spec.ts
 @ ./src sync \.spec\.ts$
 @ ./src/test.ts

WARNING in ./node_modules/rewire/lib/moduleEnv.js
require.extensions is not supported by webpack. Use a loader instead.
 @ ./node_modules/rewire/lib/rewire.js 6:16-41
 @ ./node_modules/rewire/lib/index.js
 @ ./my-function.spec.ts
 @ ./src sync \.spec\.ts$
 @ ./src/test.ts

ERROR in ./node_modules/eslint/lib/rules.js
Module not found: Error: Can't resolve '../conf/replacements' in './node_modules/eslint/lib'
 @ ./node_modules/eslint/lib/rules.js 14:25-56
 @ ./node_modules/eslint/lib/linter.js
 @ ./node_modules/eslint/lib/api.js
 @ ./node_modules/rewire/lib/moduleEnv.js
 @ ./node_modules/rewire/lib/rewire.js
 @ ./node_modules/rewire/lib/index.js
 @ ./my-function.spec.ts
 @ ./src sync \.spec\.ts$
 @ ./src/test.ts

ERROR in ./node_modules/eslint/lib/util/module-resolver.js
Module not found: Error: Can't resolve 'module' in './node_modules/eslint/lib/util'
 @ ./node_modules/eslint/lib/util/module-resolver.js 12:15-32
 @ ./node_modules/eslint/lib/cli-engine.js
 @ ./node_modules/eslint/lib/api.js
 @ ./node_modules/rewire/lib/moduleEnv.js
 @ ./node_modules/rewire/lib/rewire.js
 @ ./node_modules/rewire/lib/index.js
 @ ./my-function.spec.ts
 @ ./src sync \.spec\.ts$
 @ ./src/test.ts

ERROR in ./node_modules/require-uncached/node_modules/resolve-from/index.js
Module not found: Error: Can't resolve 'module' in './node_modules/require-uncached/node_modules/resolve-from'
 @ ./node_modules/require-uncached/node_modules/resolve-from/index.js 3:13-30
 @ ./node_modules/require-uncached/index.js
 @ ./node_modules/eslint/lib/config/config-file.js
 @ ./node_modules/eslint/lib/config.js
 @ ./node_modules/eslint/lib/cli-engine.js
 @ ./node_modules/eslint/lib/api.js
 @ ./node_modules/rewire/lib/moduleEnv.js
 @ ./node_modules/rewire/lib/rewire.js
 @ ./node_modules/rewire/lib/index.js
 @ ./my-function.spec.ts
 @ ./src sync \.spec\.ts$
 @ ./src/test.ts

ERROR in ./node_modules/rewire/lib/rewire.js
Module not found: Error: Can't resolve 'module' in './node_modules/rewire/lib'
 @ ./node_modules/rewire/lib/rewire.js 1:13-30
 @ ./node_modules/rewire/lib/index.js
 @ ./my-function.spec.ts
 @ ./src sync \.spec\.ts$
 @ ./src/test.ts

ERROR in ./node_modules/rewire/lib/moduleEnv.js
Module not found: Error: Can't resolve 'module' in './node_modules/rewire/lib'
 @ ./node_modules/rewire/lib/moduleEnv.js 5:13-30
 @ ./node_modules/rewire/lib/rewire.js
 @ ./node_modules/rewire/lib/index.js
 @ ./my-function.spec.ts
 @ ./src sync \.spec\.ts$
 @ ./src/test.ts

0 个答案:

没有答案