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

wing test #1145

Closed
eladb opened this issue Jan 12, 2023 · 2 comments
Closed

wing test #1145

eladb opened this issue Jan 12, 2023 · 2 comments
Assignees
Labels
✨ enhancement New feature or request

Comments

@eladb
Copy link
Contributor

eladb commented Jan 12, 2023

Community Note

Please vote by adding a 👍 reaction to the issue to help us prioritize.
If you are interested to work on this issue, please leave a comment.

Feature Spec

A proposal for a very simple unit testing capability that can help us raise the bar on quality. At the moment, our tests are not executing inflight code.

You can use wing test in order to execute tests that are built into the application.

Each test is a cloud function with the word test in its identifier "test".

For example:

// hello.w
bring cloud;

let b = new cloud.Bucket();
let t = inflight (e: str) => {
  print("inside inflight");
  b.put("file.txt", "hello");
  assert(b.get("file.txt") == "hello");
  assert(b.list().at(0) == "file1.txt");
};

new cloud.Function(t) as "test";

Now, if I run this:

$ wing test hello.w
2023-01-12T21:01:56.899Z {"message":"inside inflight"}
2023-01-12T21:01:56.901Z {"message":"Put (key=file.txt).","status":"success"}
2023-01-12T21:01:56.902Z {"message":"Get (key=file.txt).","status":"success","result":"\"hello\""}
2023-01-12T21:01:56.902Z {"message":"List (prefix=null).","status":"success","result":"[\"file.txt\"]"}
2023-01-12T21:01:56.902Z {"message":"Invoke (payload=\"{}\").","status":"failure","error":{}}
Error: assertion failed: '((await (await b.list()).at(0)) === "file1.txt")'
    at evalmachine.<anonymous>:37:23
    at Handler.handle (evalmachine.<anonymous>:38:15)
    at async exports.handler (evalmachine.<anonymous>:12:10)
    at async Object.withTrace (/Users/eladb/code/wing2/libs/wingsdk/lib/testing/simulator.js:107:38)
    at async main (/Users/eladb/code/wing2/apps/wing/test.js:13:3)

I can write multiple tests by simply creating additional functions:

new cloud.Function(...) as "test:bla";
new cloud.Function(...) as "test:blue";

Use Cases

Unit tests that cover both preflight and inflight.

Preflight code is executed during compilation, and this allows executing inflight as well.

Implementation Notes

Here's a little script I wrote that runs the root/test function inside a simulator. It takes in a .wsim file.

const sdk = require('@winglang/wingsdk');

async function main() {
  const s = new sdk.testing.Simulator({ simfile: process.argv[2] });
  await s.start();

  const f = s.getResource("root/test");

  s.onTrace({ 
    callback: trace => console.error(trace.timestamp, JSON.stringify(trace.data)) 
  });

  await f.invoke({});
  console.log("success");
}

main().catch(e => {
  console.error(e);
  process.exit(1);
});

Component

Other

@eladb eladb added the ✨ enhancement New feature or request label Jan 12, 2023
@eladb
Copy link
Contributor Author

eladb commented Jan 13, 2023

And naturally we need built in support for snapshot testing

https://blog.janestreet.com/the-joy-of-expect-tests

@Chriscbr
Copy link
Contributor

wing test is now available - I've created a separate issue for tracking support for snapshot tests #1214 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
✨ enhancement New feature or request
Projects
Archived in project
Development

No branches or pull requests

2 participants