Coder Social home page Coder Social logo

testing: add runIfMain about deno_std HOT 7 CLOSED

denoland avatar denoland commented on May 2, 2024 2
testing: add runIfMain

from deno_std.

Comments (7)

ry avatar ry commented on May 2, 2024 2

@bartlomieju Yes - I think if we implement window.location as described in denoland/deno#1750 then we can do

if (imports.meta.url == window.location.toString()) {
  runTests();
}

from deno_std.

ry avatar ry commented on May 2, 2024

import.meta.url is now available in 0.2.10 so runTestsIfMain should be doable now.

from deno_std.

bartlomieju avatar bartlomieju commented on May 2, 2024

I guess this belongs to testing/mod.ts but that file contains this notice: // Do not add imports in this file in order to be compatible with Node.. So import { args } from "deno"; isn't desired there. Any thoughts?

EDIT:
On the other hand

if __name__ == "__main__":
  do_something()

is a very commonly used idiom in Python. Maybe we could add runIfMain to deno itself? Then Python example would be written as:

import { runIfMain } from "deno";

runIfMain(doSomething);
// or
runIfMain(() => {
   doSomething1();
   doSomething2();
});

Just thinking out loud.

from deno_std.

bartlomieju avatar bartlomieju commented on May 2, 2024

@ry any more thoughts on this?

from deno_std.

ry avatar ry commented on May 2, 2024

FYI window.location has landed in Deno v0.3.0

One can test if the script is main by using this:

if (window.location.toString() == import.meta.url) {
  console.log("main");
}

We should be able to adapt for testing.runIfMain(import.meta)

See the manual entry.

from deno_std.

bartlomieju avatar bartlomieju commented on May 2, 2024

Closed in #215

from deno_std.

bartlomieju avatar bartlomieju commented on May 2, 2024

CC @ry

from deno_std.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.