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

Mocking uploading files with Needle does not work #244

Open
sindilevich opened this issue Aug 6, 2018 · 0 comments
Open

Mocking uploading files with Needle does not work #244

sindilevich opened this issue Aug 6, 2018 · 0 comments

Comments

@sindilevich
Copy link

sindilevich commented Aug 6, 2018

I am getting Uncaught Error: ENOENT: no such file or directory, open 'C:\Temp\mockFs\fake\base\path\file.png' when trying to mock out uploading files with needle.

System: Windows 7 x64
node.js: 10.8.0
npm: 6.3.0

needle takes file paths and uploads them onto server via HTTP POST. I am setting a file-system mock before using needle.

The file that uses needle:

// poster.ts
import needle from "needle";

export class Poster {
	public async post(file, textFile) {
		const payload = {
			values: {
				Attachment1: file,
				Attachment2: textFile
			}
		};

		return needle("post", "http://localhost:8080/api/files",
			{
				"attach-Attachment1": { file: file, content_type: "image/png" },
				"attach-Attachment2": { file: textFile, content_type: "application/json" },
				"entry": { value: JSON.stringify(payload), content_type: "application/json" }
			},
			{ multipart: true });
	}
}

And here is the test suite:

// poster-test.ts
import { expect, use } from "chai";
import chaiAsPromised from "chai-as-promised";
import mockFs from "mock-fs";
import nock from "nock";
import { Poster } from "./poster";

const poster = new Poster();

describe("Poster can...", () => {
	beforeEach("Setup Node.http.request stub and mockFs", () => {
		use(chaiAsPromised);
		mockFs({
			fake: {
				base: {
					path: {
						"file.png": "",
						"file.txt": "This is a textual file"
					}
				}
			}
		});
	});

	afterEach("Restore after mockFs", () => {
		mockFs.restore();
	});

	it("Succedes to post", async () => {
		nock("http://localhost:8080")
			.post(/^\/api.+/gi)
			.reply(200, (requestBody: string) => console.info(requestBody));
		await poster.post("fake/base/path/file.png", "fake/base/path/file.txt");
	});
});

The test is run with mocha, with extensions by chai.

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

No branches or pull requests

1 participant