postmanlabs / newman

Newman is a command-line collection runner for Postman
https://www.postman.com
Apache License 2.0
6.84k stars 1.16k forks source link

Calling Newman in Angular TypeScript application #3103

Open TheCoderGuyVlad opened 1 year ago

TheCoderGuyVlad commented 1 year ago

Hi Newman Team,

I have successfully used Newman for a while via the Mac command line and a couple times worked on some Node JS scripts that call it as a library. Both of these worked beautifully! Thank you!

But I am now trying to call Newman in my Angular UI (written in TypeScript), and I am not able to get this to work for some reason.

Could you please help me understand what I am doing wrong, or let me know if it is not possible?


Background / Assumptions: I assumed that since Newman can run in JavaScript (and TypeScript is backwards compatible with JavaScript) then it should be able to work in an Angular UI that's written in TypeScript. Also, according to this StackOverflow answer this seemed to be supported. (At least back in 2020)


Here is my Angular component code snippet:

import { Component } from '@angular/core';

// import { newman } from 'newman'; // <-- Throws error in IDE before even compiling

import * as newman from 'newman'; // <-- This is what was recommended on StackOverflow

@Component({
  selector: 'app-runner-window',
  templateUrl: './runner-window.component.html',
  styleUrls: ['./runner-window.component.css']
})

export class RunnerWindowComponent {

  constructor() {}

  ngOnInit(): void {}

  callToNewman()
  {
    newman.run( // <-- Throws error when trying to compile: "Error: Module not found: Error: Can't resolve 'newman'"
      {
        collection: "assets/test.postman_collection.json",
        reporters: "cli"
      },
      function (err: any)
      {
        if (err) { throw err; }
        console.log('collection run complete!');
      }
    );
  }
}

I have tried to load the Newman library into my project folder via the following commands:

  1. Per StackOverflow (and other sites) recommendations: npm install @types/newman --save
  2. I also tried: npm install newman and npm i -S newman, but both these caused more errors during compilation, like it was trying to recompile the whole Newman library and failing due to missing dependencies. Here are some error examples:
    Error: Module not found: Error: Can't resolve 'util'
    Error: Module not found: Error: Can't resolve 'fs'
    Error: Module not found: Error: Can't resolve 'crypto'
    ...
  3. I've tried adding all of the supposedly missing dependencies:
    npm install assert ; npm install constants ; npm install crypto ; npm install dns ; npm install fs ; npm install http ; npm install https ; npm install net ; npm install os ; npm install path ; npm install querystring ; npm install readline ; npm install stream ; npm install tls ; npm install url ; npm install util ; npm install zlib

    But sadly this didn't help either. It still gave same errors saying these modules were missing.


My version and environment information:

  1. Newman Version: 5.3.2
  2. OS details: MacOS Ventura 13.3.1 running on an Intel Mac (2018 i7 x64)
  3. Are you using Newman as a library, or via the CLI?: As a Library in an Angular application. (Angular version: 15.2.8; Node version: 18.15.0; TypeScript version: 4.9.5)
  4. Did you encounter this recently, or has this bug always been there: This is my first time trying, but judging by StackOverflow, other people were able to do this back in 2020.
  5. Expected behaviour: To be able to run a Postman collection via Newman on my Angular UI.
  6. Command / script used to run Newman: {please see code snippet above for full context, but basically I'm just using newman.run}
  7. Sample collection, and auxiliary files (minus the sensitive details): I can provide if need but issue occurs during compile time.
  8. Screenshots: ~ < > ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Using npm install @types/newman --save: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ < > ~ image ~ < > ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Using npm install newman: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ < > ~ image

Thank you for your support!

TheCoderGuyVlad commented 1 year ago

Hi @xk0der, @mcdan, @chrisscott. @ssteinerx, Could you please tell me if calling Newman from TypeScript is supported? And if so, what is the proper way to import it into my project? (The suggestions I tried online haven't worked)

Thank you!

dannyInc04 commented 1 year ago

I am not sure if this is a proper solution or not, but I added the following code in package.json and for me the issue was resolved and now I am facing issues in the run time while executing newman.run See if this makes sense for you.

"browser": {
        "fs": false,
        "path": false,
        "os": false,
        "crypto": false,
        "querystring": false,
        "http": false,
        "https": false,
        "net": false,
        "tls": false,
        "zlib": false,
        "dgram": false
    }
ViktorTyultinov commented 1 year ago

Is there an update on this issue? I am also experiencing it with the following specs:

Newman Version: 5.3.2 OS details: Windows 10 Are you using Newman as a library, or via the CLI?: As a Library in an Angular application. (Angular version: 15.0.0; Node version: 18.17.1; TypeScript version: 4.8.2) Did you encounter this recently, or has this bug always been there: Same as @TheCoderGuyVlad Expected behavior: To be able to run a Postman collection via Newman on my Angular UI. Command / script used to run Newman: Same as @TheCoderGuyVlad Sample collection, and auxiliary files (minus the sensitive details): The default request to get all collections: https://api.getpostman.com/collections

Any information on the issue would be much appreciated!