Skip to content
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

MockTestRunner & TaskMockRunner + Typescript and Mocha #1091

Open
marcopelegrini opened this issue Jan 19, 2025 · 2 comments
Open

MockTestRunner & TaskMockRunner + Typescript and Mocha #1091

marcopelegrini opened this issue Jan 19, 2025 · 2 comments

Comments

@marcopelegrini
Copy link

marcopelegrini commented Jan 19, 2025

Hi,

I've been banging my head trying to move my project to esm using Mocha + ts-node and run tests before transpiling

@DmitriiBobreshev maybe you can help me as you did a lot of work in this area here 7c0de0d

Basically, if I follow the docs: https://learn.microsoft.com/en-us/azure/devops/extend/develop/add-build-task?view=azure-devops and setup mocha + nyc I get everything working, but basically no coverage for the ts files

My assumption is that this is happening because the task is running from the dist folder rather than from the source (as setup in nyc)

Everything else in my project works fine (I can basically ignore dist during tests) and running everything from ts.

On the other hand, if I try to run MockTestRunner & TaskMockRunner with the ts files I get this:

For help, see: https://nodejs.org/en/docs/inspector
Debugger attached.
Waiting for the debugger to disconnect...
/Users/marco/Dev/typescript-node-template/test/setup/task-mock-runner.ts:1
import tmrm = require("azure-pipelines-task-lib/mock-run");
^^^^^^

SyntaxError: Cannot use import statement outside a module
    at Object.compileFunction (node:vm:360:18)
    at wrapSafe (node:internal/modules/cjs/loader:1126:15)
    at Module._compile (node:internal/modules/cjs/loader:1162:27)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1252:10)
    at Module.load (node:internal/modules/cjs/loader:1076:32)
    at Function.Module._load (node:internal/modules/cjs/loader:911:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
    at node:internal/main/run_main_module:22:47
Image

I wonder if there's anything I'm missing or this is really not supported and won't be?

Any inputs are appreciated.

Example project attached. Check commented lines for more info

Thank you

MockTestRunner-issue-with-ts.zip

@marcopelegrini
Copy link
Author

Did a little more digging and found that the issue #843 could actually fix my problem.

TaskMockRunner would work fine referencing the ts task file, but there's no way to assert what the task did... so maybe improving the TaskMockRunner would resolve it

@marcopelegrini
Copy link
Author

I've ended up writing my own TaskMocker using sinon and proxyquire which doesn't require running the task in a subprocess. Leaving it here if anyone is interested

TaskMocker.zip

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant